Methods in Java are the block of code that performs a specific task. A method represents a collection of instructions that only run when called.
The methods are very useful, especially for code reusability. We can execute the same method from different places in the code. This eliminates the need for code duplication.
Create a Method in Java
The syntax to declare a method is:
public void methodName() { // code to be executed }
Let’s see what the definition of the method contains:
- public – This is one of the Java access modifiers. Public means that the method will be visible within the entire program structure.
- void – It is the return type of the method. It means it doesn’t return any value.
- methodName – the name of the method.
Declaring a Method that Accepts Parameters
Java method can also accept parameters. When we create a method, in parentheses ( ), we will specify what parameters the method should expect.
Example:
public void methodName(int num, String data) { // code to be executed }
If a method is created with parameters, we must pass the corresponding values while calling the method.
Calling a Method in Java
In the first example, we have a method that does not accept parameters, and we will call such a method as follows:
methodName()
As you can see, to call a method, we need to specify its name and leave the parentheses empty if it does not accept the parameters.
Calling a Java method with parameters
To call a method with parameters, we must pass the corresponding values inside parentheses.
methodName(10, "dataString");
In the above example, we passed two parameters, the first is a number of type int, and the second is just text of type String.
Let’s see a realistic, slightly more complicated example:
class Test { public int sumNumbers(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) { int num1 = 10; int num2 = 15; Test test = new Test(); int result = test.sumNumbers(num1, num2); System.out.println("The sum is: " + result); } }
Types of Methods in Java
In Java, there are two types of methods:
- User-defined Methods: The methods we create
- Predefined Methods/Standard Library Methods: Built-in methods in Java that are available to use.
User-defined Methods
A method that we write ourselves is known as a user-defined method. In the above examples, we have already seen how we can create our own methods.
Here is one more example:
class Test { public static int square(int num) { return num * num; } public static void main(String[] args) { int result = square(10); System.out.println("The result is: " + result); } }
Predefined Method in Java
Predefined methods are those that come as part of existing Java packages and libraries. These methods are available for us to use.
We have already seen one of these: the println method of the PrintStreamclass. We have many other classes with various methods available.
Let’s see an example of methods from the Math class:
class Test { public static void main(String[] args) { int maxOfTwoNumbers = Math.max(2, 10); int minOfTwoNumbers = Math.min(2, 10); double sqrt = Math.sqrt(10); System.out.println("Max is: " + maxOfTwoNumbers); System.out.println("Min is: " + minOfTwoNumbers); System.out.println("The square root is: " + sqrt); } }