Java - Difference Between Static and Non- static Methods

Java | Static Vs. Non-static Methods: In this tutorial, we will learn about the static and non-static methods in Java and the difference between static and non-static methods. By Preeti Jain Last updated : March 25, 2024

Java Static Methods

  • We must use static keywords to declare or define static methods.
  • Static methods are associated with the class that means these methods can be called with the class name or with objects or without objects(Direct call) there is no need to declare any objects for static methods.
  • Static methods can access only static member and static methods are of the same class or different class that means static method can’t access non-static member or methods.
  • Static methods create only one copy of the whole program and share it with other member or methods.

Example of Java Static Methods

// Show the behavior of static member and methods  
class StaticClass {
    // declare a static method 
    public static int div(int a, int b) {
        return a / b;
    }
}

public class Main {
    public static void main(String[] args) {
        int p = 10, q = 5;

        // Declare a static member
        String str = "Welcome in Java World";

        // Invoking the static method with the classname
        int div = StaticClass.div(p, q);

        /* 
         Here we are calling static methods directly without 
         objects or classname. If we want to call with classname 
         or with object then also not a problem. 
        */
        System.out.print("The value of str is = " + str);
        System.out.print("The divide of two number is = " + div);
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
The value of str is = Welcome in Java World
The divide of two number is = 2

Java Non-Static Methods

  • We must not have static keywords before method name to declare or define static methods.
  • Non-static methods are not associated with the class that means these methods cannot be called with the class name and it is mandatory to declare objects and non-static methods can be called with the object name.
  • Non-static methods can access static member and static methods and non-static member and non-static methods are of the same class or different class but non-static can’t modify static member or methods values.
  • For non-static methods create an individual copy of all the objects or in other words create a separate copy of all the objects.

Example of Java Non-Static Methods

class NonstaticClass {
    // Declare a non-static method 
    public int div(int a, int b) {
        return a / b;
    }
}

public class Main {
    public static void main(String[] args) {
        int p = 10, q = 5;

        // Declare an object of NonstaticClass
        NonstaticClass nc = new NonstaticClass();

        int div = nc.div(p, q);

        // Invoking the non-static method with the class object.
        System.out.print("The div of two num is = " + div);
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
The div of two num is = 2

Difference Between Static and Non- static Methods in Java

Below table shows the differences between static and non-static methods in Java:

Feature Static Method Non-Static Method
Invocation The static methods can be accessed using the class name. The non-static methods can be accessed using an object instance.
Memory Allocation The static methods are stored in method area of JVM memory. The non-static methods are stored in heap memory.
Access to Instance Members The static methods cannot access instance variables directly. The non-static methods can access instance variables directly.
Access to Static Members The static methods can access static variables and methods directly. The non-static methods can access static variables and methods directly.
"this" Keyword The static methods cannot use "this" keyword inside the method. The non-static methods can use "this" keyword to refer to the object.
Overriding The static methods cannot be overridden. The non-static methods can be overridden in child classes.
Utility Methods The static methods commonly used for utility methods. The non-static methods typically used for operations on object state.
Dependency on State The static methods are typically stateless. The non-static methods can depend on and modify object state.
Thread Safety The static methods may not be thread-safe if stateful. The non-static methods may or may not be thread-safe depending on state.
Inheritance The static methods cannot be abstract or final. The non-static methods can be abstract or final.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.