In Java, a method is nothing but a block of code/statement that is declared within the class and can perform different actions when someone calls it. Some methods can be called directly with their name (i.e. without creating the class object) while some methods require instance/object of the class (i.e. must be invoked with the object of the class).

The methods that can be called directly are referred as a class or static methods, while the methods that need an object of the class to be invoked are referred as instance or non-static methods.

This write-up will present a detailed overview of class methods and in this regard, it will cover the following aspects of Java class methods:

Let’s get started!

Class Method in Java

Generally, when we have a class then we have to create an object of that class to access its methods and other members. However, the class/static methods can be accessed inside of the class without creating an instance of that class.

How to Access Class Methods

Let’s consider the below-given example to understand how to create and access a static/class method in Java.

Example

The below code snippet takes two numbers from the user and perform addition on them:

publicclassAddNumbers {

publicstaticintaddition(int num1, int num2) {

int add = num1 num2;

return add;


    }

publicstaticvoidmain(String[] args) {

int number1, number2, sum;


        Scanner scan = new Scanner(System.in);

System.out.print(“Enter 1st number: “);


        number1 = scan.nextInt();

System.out.print(“Enter 2nd number: “);


        number2 = scan.nextInt();


        sum = addition(number1, number2);

System.out.println(“Sum = “ sum);


    }

}

The complete code and its respective output will be something like this:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-2.png" data-lazy- height="731" src="data:image/svg xml,” width=”975″>

From the above output, it is clear that there is no need to create the object of the class to call a static method instead it can be accessed directly within the class.

How to Access Public Methods

Now let’s consider the below example to test whether a public method can be accessed directly or not:

publicclassAddNumbers {

publicintaddition(int num1, int num2) {

int add = num1 num2;

return add;


    }

publicstaticvoidmain(String[] args) {

int number1, number2, sum;


        Scanner scan = new Scanner(System.in);

System.out.print(“Enter 1st number: “);


        number1 = scan.nextInt();

System.out.print(“Enter 2nd number: “);


        number2 = scan.nextInt();


        sum = addition(number1, number2);

System.out.println(“Sum = “ sum);


    }

}

All the code is the same as in the previous example except the access modifier, but this time we get an error as shown in the following code snippet:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-7.png" data-lazy- height="622" src="data:image/svg xml,” width=”975″>

To access a non-static function, first, we have to create the object of the class then we will be able to access the method of the class:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-15.png" data-lazy- height="809" src="data:image/svg xml,” width=”975″>

The above snippet verifies that when we call the non-static method with the help of a class object then it works appropriately and provides the error-free output.

How to Access a Method from a Different Class

We have seen that a static method doesn’t require any object to be called within the same class but what will happen when we have multiple classes? Will the static method be invoked directly in such a case? Let’s experiment with it!

Example

Let’s consider we have two class: one class named “AddNumbers” which will hold the main method and the second one is “MyFunctions” class:

MyFunctions.java

packageaddnumbers;


publicclassMyFunctions {

publicstaticintaddition(int num1, int num2) {

int add = num1 num2;

return add;


    }

}

AddNumbers.java

publicclassAddNumbers {

publicstaticvoidmain(String[] args) {

int number1, number2, sum;


        Scanner scan = new Scanner(System.in);

System.out.print(“Enter 1st number: “);


        number1 = scan.nextInt();

System.out.print(“Enter 2nd number: “);


        number2 = scan.nextInt();


        sum = addition(number1, number2);

System.out.println(“Sum = “ sum);


    }

}

We call the addition function of the MyFunctions class from the main method of AddNumbers class:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-17.png" data-lazy- height="476" src="data:image/svg xml,” width=”975″>

Although the addition method is static but we still get an error when we try to access it directly. This is because the addition method is not in the same class. So, to access the method of some other class we have to create the object of that class irrespective of its access modifier i.e. static or public.

AddNumbers.java

publicclassAddNumbers {


publicstaticvoidmain(String[] args) {

int number1, number2, sum;


        Scanner scan = new Scanner(System.in);

System.out.print(“Enter 1st number: “);


        number1 = scan.nextInt();

System.out.print(“Enter 2nd number: “);


        number2 = scan.nextInt();


MyFunctions obj = newMyFunctions();


        sum = obj.addition(number1, number2);

System.out.println(“Sum = “ sum);


    }

}

This time we create the object of MyFunctions class in the main function of AddNumbers class and then we access the addition method with the help of that object:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/word-image-18.png" data-lazy- height="734" src="data:image/svg xml,” width=”975″>

Now the above snippet verifies that the error has gone, and with the help of the object of MyFunctions class we got the desired results.

Conclusion

The class/static method can be accessed within the class directly while accessing the public methods without creating the object is not possible. While, in the case of multiple classes, the methods will be accessible only with the help of class objects regardless of their access modifier. This write-up provides a comprehensive guide of what are class methods how to access them from the same class and from a different class.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg622549407964f.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.