Method Creation and Usage in Java

Method Creation and Usage in Java Featured Image

Once again, welcome back! The topic for today is Java methods. If you have not been following along, we have covered logic and binaryprimitive types, and reference types. Now, we’re going to dive into one of the features of objects called method. In particular, we are going to focus on method creation and usage in Java.

Table of Contents

What Are Methods?

If you’ve had a chance to play with strings, then you’re probably familiar with a few methods like `length()`java and `equals()`java. In fact, last lesson we covered methods very briefly when we mentioned the dot operator.

As it turns out, methods are actions we perform on objects. In the case of strings, we’ve used methods to learn about various `String`java objects. For instance, we can use `length()`java to determine the number of characters in a `String`java object.

Of course, methods can do other things like manipulate objects. Since strings are immutable, there typically aren’t many of those kinds of methods. However, they do exist.

For instance, there is a `String`java method called `toLowerCase()`java which will convert all the characters in a `String`java object to lowercase. As already mentioned, strings are immutable, so the method will actually return a brand new `String`java object—one with a new address. Let’s take a look:

String myString = "Hello, World!";
myNewString = myString.toLowerCase();
System.out.printLn(myString); // Prints "Hello, World!"
System.out.printLn(myNewString); // Prints "hello, world!"

Notice how the two strings are different. That’s because the original `String`java object was never changed. Instead, the `toLowerCase()`java method returned an entirely new `String`java object. We’ll dig more into this later.

Until then, we should probably touch on an important Computer Science topic: the stack.

The Stack

As we continue these lessons, we will want to become familiar with two key areas of computer hardware: the stack and the heap. These two concepts define regions of memory where a program lives.

When we talk about methods, we really only care about the stack. The stack is a last-in-first-out (LIFO) data structure which closely resembles something like a pancake stack (except it is often drawn upside down). As you might imagine, the last pancake added to the stack gets eaten first.

Every time we use a method, it gets added to the stack as a method call. For instance, if we call `equals()`java on a String object, a new method call will get added to the stack. Once `equals()`java is finished computing, the call pops off the stack, and we’re back to an empty stack.

For the sake of argument, let’s say that `equals()`java calls a few methods of its own. For instance, maybe it compares the two strings by length. If they’re the same length, we’ll say the strings are equal. To do this, `equals()`java would have to wait for `length()`java to finish computing on both strings before it could return `true`java or `false`java.

Fortunately, the stack simplifies the complexity a bit. The program will begin by pushing the `equals()`java call onto the stack. Then, the program will push the `length()`java call for the first string on top of the `equals()`java call.

Once `length()`java is finished, it will pass the result to `equals()`java, and the program will push an additional `length()`java call on top of the `equals()`java call for the second string. Finally, `equals()`java will use the result of both `length()`java calculations to report `true`java or `false`java.

Don’t worry if this seems over your head! We will revisit stacks in the future.

A Tale of Two Methods

In general, there are two basic types of methods: static and instance. So far, we have been working with instance methods which operate on an instance of a class. For example:

String favoriteColor = "blue";
String favoriteFood = "sushi";
favoriteColor.equals(favoriteFood);

Here, we have two instances of the class `String`java: `favoriteColor`java and `favoriteFood`java. The first thing to note is that the `equals()`java method makes use of both of these variables. However, only one of these variables is supplied as an input while the other is initiating the method call.

So how does this work?

Instance methods work because the object they are a part of provides context to the operation. In other words, we only need one input for the equals method because it has access to the calling object.

Meanwhile, a static method belongs to the class only. In other words, static methods do not have access to instance properties like the character sequence that defines `favoriteColor`java. Instead, static methods provide utility to a class as a whole. For instance:

String.valueOf(5);

This line calls the static method `valueOf()`java from the `String`java class on an integer. The result is the integer as a `String`java or “5.” Static methods work without ever needing an instance of a class because they don’t need any context to perform their function.

Method Syntax

Now that we have an idea of how methods are used, let’s take a look at how they are written.

Static Method Syntax

First, let’s look at a static method:

public static int add(int firstNum, int secondNum) {
    return firstNum + secondNum;
}

In the first line, we have quite a few pieces of information:

  1. `public`java: indicates that the method is public (we can discuss this later)
  2. `static`java: declares the method as static
  3. `int`java: indicates a return type of integer

The next bit is the method name followed by parentheses which contain the inputs. In this case, we have created an add method with two `int`java inputs.

Inside the body of the method, we have the return statement. The `return`java keyword marks an exit point for the method. In other words, return takes whatever is on its line and returns it as output from the method. If we were to call `add(5, 6)`java, the return line would give us a result of 11.

You may also notice that there are a couple of pieces of interesting syntax. For instance, the line in the body ends with a semicolon, `;`java. In Java, all lines must end with a semicolon unless they open a code block. Code blocks are opened using an open brace, `{`java, and closed using a close brace, `}`java.

Instance Method Syntax

Unfortunately, until we talk about class layout, we won’t have any static method examples for you to try on your own. However, you can try writing your own instance methods which will serve a similar purpose for now.

Instance method syntax is exactly the same except you exclude the `static`java modifier. For instance:

public int add(int firstNum, int secondNum) {
    return firstNum + secondNum;
}

Try entering this directly into the DrJavaOpens in a new tab. interactions pane. If successful, you should be able to call the method using its name and two inputs of your choice. Try using your knowledge of the primitive types to observe the method under various inputs. Some examples might include:

add(2, 3)
add(-4, 6)
add(4.0, 7)
add(5, true)

Notice how some of these examples fail where they might not have using the addition operator. That’s because our method enforces integer input only.

So what makes this method different from a static method?

At the moment, nothing. However, try the following:

int firstNum;
int secondNum;
public int add() {
    return firstNum + secondNum;
}

Now you can call add without any inputs. Without explicitly defining `firstNum`java and `secondNum`java, `add()`java should return 0. However, you can change the behavior of the add method by changing either of those variables. Try modifying `firstNum`java and monitoring the behavior of add.

Method Overloading

Without realizing it, you have just overloaded the method add. Not sure what that means? Try calling add both ways: once with no inputs and once with two inputs.

You have just created two different add methods that both share the same name. This is perfectly legal in Java, and it allows us to expand the behavior of a single method to handle multiple input parameter lists. There are many uses for method overloading, but in general it is used to improve code readability.

In the future, we will definitely tackle code readability as I feel it is the most important aspect of coding. For now, we’re going to stop here. Thanks again for sticking around! Up next we’ll be widening our scope to class structure, so we can start writing our own code.

Java Basics (10 Articles)—Series Navigation

The Java Basics series is a beginner friendly tutorial series which covers topics such as binary, logic, control flow, and loops. By the end of the series, students should feel confident enough to write a simple application. In addition, students can head straight into the data structures series.

As with any series, I’m always looking for feedback. If you felt like something was missing or everything was excellent, let me know in the comments below each lesson. As always, if there’s anything else you want to see, I’m happy to write an article about it.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts