Java

Class Methods in Java | Explained

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:

public class AddNumbers {

    public static int addition(int num1, int num2) {
        int add = num1 + num2;
        return add;
    }

    public static void main(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:

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:

public class AddNumbers {

    public int addition(int num1, int num2) {
        int add = num1 + num2;
        return add;
    }

    public static void main(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:

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:

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

package addnumbers;
public class MyFunctions {

    public static int addition(int num1, int num2) {
        int add = num1 + num2;
        return add;
    }
}

AddNumbers.java

public class AddNumbers {

    public static void main(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:

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

public class AddNumbers {
    public static void main(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 = new MyFunctions();
        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:

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 and how to access them from the same class and from a different class.

About the author

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.