how to call a method in java

How to call a method in Java

A method is a collection of statements that are grouped together to perform a specific task. Methods are a key part of object-oriented programming and provide a way to modularize code. In Java, methods are defined within classes and are accessed using the class name followed by the method name.

How to call a method in Java

To call a method in Java, you need to know the following:

  1. The name of the class that contains the method.
  2. The name of the method.
  3. The parameters of the method, if any.
  4. The return type of the method.
  5. The order of the parameters.

Let’s look at an example. The following code snippet defines a class called Employee and contains a method called calculatePayment().

class Employee {

public static void calculatePayment(int hours, int rate) {

double payment = hours * rate;

System.out.println(“Payment for ” + hours + ” hours at ” + rate + ” per hour is ” + payment);

}

}

Parameters of a method

A method can have zero or more parameters. The parameters of a method are the values that are passed to the method when it is called. In the example code snippet, the calculatePayment() method takes two parameters, hours and rate.

The parameters of a method are always specified in the order in which they are to be passed. In the example code snippet, the first parameter is hours and the second parameter is rate.

Return type of a method

The return type of a method is the type of the value that is returned by the method. In the example code snippet, the return type of the calculatePayment() method is double. This means that the method returns a value of type double.

When a method returns a value, the value is assigned to a variable that is defined in the calling code. In the example code snippet, the payment variable is defined in the calling code and it is of type double. This means that the value returned by the calculatePayment() method is assigned to the payment variable.

Overloading a method

A method can be overloaded, which means that the method has multiple signatures. A signature is a combination of the name of the method, the number of parameters, and the type of the parameters. In Java, the method name and the number of parameters must be the same, but the type of the parameters can be different.

In the example code snippet, the calculatePayment() method is overloaded. The first signature takes two parameters, hours and rate, and the second signature takes one parameter, hours.

Method invocation 

The process of invoking a method is the same for both static and non-static methods. The only difference is that for static methods, the class name is prefixed to the method name, whereas for non-static methods, the object reference is prefixed to the method name.

When a method is invoked, the following steps are executed:

  1. The Java Virtual Machine searches for the method in the class that contains the source code of the program.
  2. Searches for the method in the superclasses of that class.
  3. Searches for the method in the superclasses of the superclasses of that class.
  4. Searches for the method in the Java Class Library.

If the method is not found, an error is thrown.

Conclusion 

Methods are the basic units of functionality in Java. They are declared in a class and are invoked on objects of that class or its subclasses. A method is overloaded when it has the same name but different argument types. When a method is invoked, the Java Virtual Machine searches for the method in the class that contains the source code of the program, and then in the Java Class Library. If the method is not found, an error is thrown.