In this tutorial, we will learn about the method in Java, how to define a method which can take a parameter or not, and also in case of a return data type or not for that method. We will fully learn about it.
Components of a method include:
- Data type returned.
- Parameters of the method.
- Return statement
- The access and non-access modifiers.
The doAdd example below is a method:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava; public class Example { public int doAdd(int a, int b) { int c = a + b; return c; } } |
We have learned about access and non-access modifiers in previous tutorial, so in this tutorial we will not mention them.
Data type returned
A method may or may not have a return value.
When they are defined with no return value, the return value of the method is void.
For example:
1 2 3 4 5 6 7 8 |
package com.huongdanjava; public class Example { public void printMessage() { System.err.println("Welcome to Huong Dan Java!"); } } |
When they are defined with a return value, the return value of the method can be a primitive value or an object of any class. The name of the return type can be one of eight primitive types or the name of any class or interface.
Back to the example at the beginning of this tutorial, we have the doAdd() method return a primitive value and the name of its return type is int.
When a method defines the return type, when calling that method we can assign its return value to a variable with a type value as the return value of the method.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava; public class Example { public int doAdd(int a, int b) { int c = a + b; return c; } public static void main(String[] args) { Example example = new Example(); int total = example.doAdd(3, 4); System.out.println(total); } } |
With no return value, we cannot assign it a variable, it will be compiled error if we try to do it:
Parameters of the method
Method parameters are variables declared in the definition of the method that specifies the type and value that the method can use. These parameters are declared separated by commas.
The doAdd() method in the first example of this tutorial has two parameters a and b that are declared with the same int value.
We can no pass or pass many parameters to a method and we can also declare the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava; public class Example { public int doAdd(int... values) { int c = 0; for (int i = 0; i < values.length; i++) { c = c + values[i]; } return c; } public static void main(String[] args) { Example example = new Example(); int total = example.doAdd(2, 3); System.out.println(total); } } |
The doAdd() method in the example above declares a parameter with three dots after the type value of the parameter. Three dots mean the variable values are either an array or a string of values separated by commas. And so we can pass many values to the method with the same type of value that we have declared.
The definition of the three dots is only true when we define this parameter at the end of the method parameter list. If we try to do otherwise, we will get the error right away:
Return statement
The return statement is used in a method, which may include a value or not.
The return statement with a value used in a method having return data type and the value follows the return statement is the return value of the method.
In the first example of the tutorial, we used the return statement with the value followed by the sum of the two numbers a and b.
With a return statement without a value, it is used to escape a method.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava; public class Example { public void printMessage(String name) { if (name == null) { return; } System.err.println("Welcome to Huong Dan Java: " + name); } } |
In this example, if the name is null, the following lines of code will not be processed because the return statement has exited the method when calling it.
We cannot put a return statement at the beginning of a method when the method does not process any statements.
The following example shows you the code that will cause compile errors if we try to do it: