A method or function of any programming language contains a block of code or statements that need to execute multiple times in the program. The same code can be reused multiple times by declaring a method with that code and calling the method. The method is the major part of the class declaration in Java. The types of methods, access modifiers of the method, and the way of declaring and using the method in Java are shown in this tutorial.
Prerequisites:
- Install the latest version of OpenJDK with JRE.
- Install any useful editor to write and execute the Java code. You can check this tutorial to install the Eclipse editor.
Types of Method:
Two types of methods can be used in Java. These are mentioned as follows:
1. Built-In Method
Built-in methods are already defined in Java libraries such as random(), equals(), etc. These methods can be used by calling the method.
2. User-Defined Method
User-defined methods are implemented by the Java user based on the programming requirements. These methods are required to be defined inside the class before use.
Access Specifiers of the Method
Four access specifiers can be used to set the scope of the methods in Java. The functions of these access modifiers are explained in the following:
Access Modifier | Function |
---|---|
Default | When no specifier is declared for the method, the default specifier is used. The method is accessed inside the Java package only for the default specifier. |
Public | The method of the class is accessible from anywhere if the public specifier is used. |
Private | The method of the class is accessible inside its class only if the private specifier is used. |
Protected | The method of the class is accessible inside its class and the subclass only if the protected specifier is used. |
Java Method Examples
The uses of built-in and user-defined methods are shown in this part of the tutorial using multiple Java examples.
Example 1: Using the Built-In Method
Create a Java file with the following code where the uses of two built-in methods are shown. One is the print() method which prints the output in the console. Another is the pow() method which calculates the power value of 3 to the power 4:
The following output appears after executing the previous code. The power value of 3 to the power 4 is 81 which is printed in the output:
Example 2: Using the User-Defined Method without Argument
Create a Java file with the following code where a user-defined method without any argument is declared to print the content of the two class variables. The class variables are initialized inside the main() method and the user-defined function is called later to print the values of the variable.
//Declare two class variables
public String name;
public String email;
//Declare a user-defined method without argument
public void show()
{
//Print the variables
System.out.println("Name: " + this.name);
System.out.println("Email: " + this.email);
}
public static void main(String[] args)
{
//Declare an object of the class
UseOfJavaMethod2 Obj = new UseOfJavaMethod2();
//Assign values to the class variables
Obj.name = "Mehzabin Chowdhury";
Obj.email = "[email protected]";
//Call the method
Obj.show();
}
}
The following output appears after executing the previous code:
Example 3: Using the User-Defined Method with Argument
Create a Java file with the following code where a user-defined method with two arguments is declared to compare two numeric values and print a message based on the comparison. The method is called from the main() method with the argument values, 67 and 34.
//Declare a user-defined method with two arguments
public void check(int a, int b)
{
//Compare the argument values
if (a > b)
System.out.println(a + " is " + " greater than "+ b);
else
System.out.println(a + " is " + " less than "+ b);
}
public static void main(String[] args)
{
//Declare an object of the class
UseOfJavaMethod3 Obj = new UseOfJavaMethod3();
//Call the method with two values
Obj.check(67, 34);
}
}
The following output appears after executing the previous code. Sixty-seven (67) is greater than 34 and it is printed in the output:
Example 4: Using the User-Defined Method with Return Value
Create a Java file with the following code where a user-defined method with three arguments is declared to calculate the average of three numbers and the result to the caller. The return statement is used to return the value from the method. Numbers 5, 7, and 3 are passed as the argument values of the method in the code.
//Declare a user-defined method with a return type
public float average(int a, int b, int c)
{
//Calculate the average value of three numbers
float average = (a + b + c)/3;
//Return the calculated value
return average;
}
public static void main(String[] args)
{
//Declare an object of the class
UseOfJavaMethod4 Obj = new UseOfJavaMethod4();
//Print the return value
System.out.println("The average value is "+ Obj.average(5, 7, 3));
}
}
The following output appears after executing the previous code. The average of 5, 7, and 3 is 5.0 which is printed in the output:
Conclusion
The method is an essential part of any programming language. Java has many built-in methods to perform different types of tasks. Custom methods are used to solve particular programming problems. The uses of built-in methods and different types of user-defined methods are described here to help the new Java users. The argument of the method can be also called in different ways. The way of calling the methods with the arguments by values are shown in this tutorial.