| by Arround The Web | No comments

Different Ways to Call a Method in Java

In Java, a method is a combination of logical statements utilized to perform specific operations. Methods are commonly used because their added code can be reused as often as needed. Each method in Java is called using its name, and when the Java compiler reads it, the mentioned method is invoked, and the added operational code gets executed.

This tutorial will illustrate different ways of calling methods in a Java program.

Different Ways to Call a Method in Java

In Java, you can call a method using:

  • Class Object
  • Class name

We will now check out both of the mentioned methods in detail.

How to Call a Method Using Class Object?

There are two types of methods that can be invoked using the class object:

  • predefined method 
  • user-defined method

Have a look at the below-given sections to understand the implementation for both methods.

Method 1: Calling Predefined Method Using Class Object

The predefined or built-in methods in Java are already defined in the Java Classes and act as Java libraries. For instance, the print() method defined in the java.io.PrintStream class is used to print the added statements.

Example

In this example, we will call the predefined method hashCode() of the Object class by creating an object called hash.

To do so, first, we will create a variable name ob:

int ob;

Then create an object hash of the Object class, call the predefined hashCode() method by using the dot (.) operator, and save it in the created variable. The hashCode() returns the unique number of the object that is generated by JVM:

Object hash = new Object();
ob=hash.hashCode(); 

Finally, print the value of hashCode, which is stored in variable ob:

System.out.println("Hash Code of the object is: "+ob);


The given output indicates that we have successfully accessed the hash code of the created object using the “hashCode()” method:

Method 2: Calling User-defined Method Using Class Object

A user-defined method is a type of method that is written by the programmer. These methods permit the users to define functions or methods according to your program requirements.

Example

We will create two Java classes named JavaClass1 and Example. JavaClass1 contains two user-defined methods named Sum() and Sub(). The Sum() method is the static method, while Sub() is the public method:

class JavaClass1 {
    static int x=20,y=23;
    static int Sum() {
        return x+y;
    }
    public int Sub(){
       return x-y;
    }
}

Note: To access the static method, there is no need to create an object of the class while for accessing the public method we need to create an object of the class.

By creating an object of JavaClass1 in the Example class, we can access the static and public methods of JavaClass1, as shown below. However, the Sub() method is at the top because it is defined as a public method:

public class Example {
    public static void main(String[] args) {
        JavaClass1 jc = new JavaClass1();
        int ans= jc.Sub();
           System.out.println("The difference of two numbers: "+ ans);   
    }
  }

Output

Now, let’s head towards the second approach for calling a method using the class name.

How to Call a Method Using Class Name?

With the help of the class name, you can call the following methods:

  • predefined static method 
  • user-defined static method 

The following sections will discuss the procedure of invoking the mentioned methods using the class name.

Method 1: Calling Predefined Static Method Using Class Name

As discussed above, there is no need to create an object to call the static methods of the class. For this purpose, simply utilize the class name with the method and pass an argument if required.

Example

In this example, we will invoke the predefined static sqrt() method of the Math class and pass “100” as an argument:

public class Example {
 public static void main(String[] args) {
        double sqr;
    sqr = Math.sqrt(100);
        System.out.println("The square root of 100 is: " + sqr);
    }
}


The output of the above program will display “10.0” as the square root of “100”:

Method 2: Calling User-defined Static Method Using Class Name

We can also create user-defined static methods and access them by using the class name.

For example, in the given code block, JavaClass1 is the Class, and Sum() is the user-defined static method of the class JavaClass1:

JavaClass1.Sum();

Example

In this example, we will utilize the already created JavaClass1 and Example classes. The strategy is to call the Sum() method of JavaClass1 in the Example class using the class name:

public class Example {
 public static void main(String[] args) {
        int sum= JavaClass1.Sum();
        System.out.println("The sum is: " + sum);
    }
}

Output


We have compiled all of the essential information related to calling a method in Java.

Conclusion

There are two ways to call the method in Java: using the class name and using the class object. The static methods of the class can be accessed by using the class name, while the public methods of the class can be accessed by creating an object of the class. We can access static and public methods, whether they are predefined or user-defined methods. In this tutorial, we illustrated the different ways to call a method in Java in detail with examples.

Share Button

Source: linuxhint.com

Leave a Reply