In this article, we will learn how to call a method in Java. A method is a set of instructions, created to perform a specific task. Instead of writing the same set of instructions again and again, we simply create a method in java that has those instructions inside the method body and this method can be called whenever we need to perform that specific task. This facilitates the reusability in our code as we don’t have to write same set of statements multiple times.
How to call a method in Java
Let’s take a simple example first to understand how to create a method and then call it. In the following example, we have created a simple method hello()
that just prints a text message and we have called the method inside the main() method of the same class. As you can see we have to create the object of the class first and then we called the method using the object.
public class JavaExample { // A method public void hello() { System.out.println("Hey, I'm just a method."); System.out.println("Hello"); } public static void main(String[] args) { //Creating the object of the class JavaExample obj = new JavaExample(); //Calling the method obj.hello(); } }
Output:
Hey, I'm just a method. Hello
Calling methods with multiple parameters
In the above example, we learned how to call a method with no arguments. In this section, we will learn how to call a method with parameters by passing arguments. In the following example, we have created a method with two int parameters, this means whenever we call this method, we have to pass two integer parameters.
public class JavaExample { public int add(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { JavaExample obj = new JavaExample(); //Calling the method int sum = obj.add(10,5); System.out.println("Sum: " + sum); } }
Output:
Sum: 15
How to call a static method in Java
A static method in java can be called without creating the object of the class. Here we will learn how to call a static method in Java. To learn more about static method refer this guide: Static method in Java.
We can call a static method by using ClassName.MethodName
. For example, class Math
contains a static method max(int a, int b)
that returns the largest number when two numbers are passed as an arguments to it. We can call this method by using Math.max(int a, int b)
.
Let’s have a look at the complete code:
import java.util.Scanner; public class JavaExample { public static void main(String args[]) { int num1, num2, max; Scanner sc = new Scanner(System.in); System.out.println("Enter First int number:"); num1 = sc.nextInt(); System.out.println("Enter Second int number:"); num2 = sc.nextInt(); sc.close(); //Here we are calling the static method max of Math class max = Math.max(num1,num2); System.out.println("Max number between "+num1+" and "+ num2+" is: "+ max); } }
Output:
Enter First int number: 22 Enter Second int number: 8 Max number between 22 and 8 is: 22 Process finished with exit code 0
Explanation: As you can see in the above example, we called a static method max()
that belongs to the Math
class, we didn’t need to create the object of the Math
class and we simply used ClassName.MethodName
to call the static method.
Notes:
- We can create multiple methods with the same name but different number, sequence or type of arguments using method overloading.
- We can also override the method in subclass which has already been declared in the parent class. This is known as method overriding and to learn this concept in details with examples refer this guide: Method overriding in Java.
Leave a Reply