Java methods

July 29, 2016

In Java there is a type of entity called a method. Methods help us in organizing our code. A method consists of a header and a body. Let’s have a look at the example of a method:

1. Method header

1.1. Method modifiers

The first line of a method is a header. It contains important information about a method. The first two words public static are modifiers. We will cover this topic more accurately later in this tutorial. Until I explain what it exactly means, just remember that with every method you create, you should now use these public static modifiers.

1.2. Method return type

The third word you see in this method is the int keyword. When it’s placed in this place, it defines a type of variable returned by a method. So you can return from a method any type you want. And you don’t have to return any type. Then instead of a return type you use the keyword void.

1.3. Method name

The fourth word is add. This word indicates method name. Later we can call this method using this name.

1.4. Method parameters

Next in the method you can see an opening bracket. Inside brackets, we give parameters that are passed into methods. So in general, you can pass variables to the method and you can later operate on them. When you declare parameters that are passed to a method, you have to declare their types and names, which you can later use inside this method. You can pass as many parameters to a method, as you want. Every parameter you separate by comma. You don’t have to pass any parameter to the method. Then after a method name you just put an opening and closing bracket like this: ().

In this example, we passed 2 parameters to our method. The first one is of type int and has the name firstNumber. The second one is also of type int and has the name secondNumber.

2. Method body

2.1. Curly brackets

We put a method body in curly brackets. So as you can see the first line of above code ends with an opening curly brace {. And the last line of this code ends with an ending curly brace }. What’s between them is a method body.

2.2. Code inside method body

Inside a method body, you can place your code. The first line of code inside method body in above code is:

So as you can know from this lesson, we declare here variable of type int named sum. Later we assign to this variable a sum of two numbers: firstNumber and secondNumber.

2.3. Returning value from method

The second line of the method body is:

We use return keyword in Java to return some value. So our method will return the value stored inside variable named sum. Remember about it, that the value you return from a method, needs to have exactly the same type as the return type declared in a method header.

2.4. Returning directly from method

Actually, in the example above, we haven’t had to declare variable sum. So instead of having two lines of code inside body of our method:

We can have had only one:

And our method will work in this case exactly the same. So it will return the sum of firstNumber and secondNumber.

3. How to call method in Java

To call a method in Java you give a method name and after that, you pass parameters inside brackets. Let’s create a new class in Eclipse named MethodCallExample. After creating it, change its content like in the following listing:

In the the first line of the main method we have:

In this line we declare the variable named sumInMain of type int. Then we assign to this variable the value returned by the method named add. To the method add we pass 2 parameters: values 1 and 2.

This cause that our add method is called. The value 1 is assigned to the method parameter firstNumber. And the value 2 is assigned to the variable named secondNumber. In the first line of the add method:

the value of these 2 parameters is added and the value 3 is assigned to the variable sum.

Then in the last line of the method add:

the value 3 is returned from the method add.

After that we come back from the method add to this line in the main method:

And we assign the value returned by the add method, to the variable sumInMain.

After that, we print the content of the variable using System.out.println() method.

So if you run this method, the result will be like this:

Click here to add a comment

Leave a comment: