Open In App

Static Method vs Instance Method in Java

Last Updated : 22 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, methods are mainly divided into two parts based on how they are connected to a class, which are the static method and the Instance method. The main difference between static and instance methods is listed below:

  • Static method: A static method is a part of the class and can be called without creating an object.
  • Instance method: Instance method belongs to an object, we need to create an object to use instance methods.

Difference Between Static Method and Instance Method

The following table lists the major differences between the static methods and the instance methods in Java.

Features

Static method

Instance method

Definition

Created using the static keyword and retrieved without creating an object.

Requires an object of its class to be invoked.

Access

Access only static variables and methods.

Can access both static and instance members.

this keyword

Cannot use the this keyword within static methods.

Can use the this keyword to refer to the current object.

Override

Does not support runtime polymorphism

Supports runtime polymorphism

Memory Allocation

Stored in Method Area (PermGen space in Java 7 and earlier, replaced by Metaspace in Java 8+)

Stored in Method Area (PermGen/Metaspace)

Static Method

A static method can be created using the static keyword. It can be called without creating an object of the class, referenced by the class name itself, or a reference to the object of that class. 

Memory Allocation of Static Methods

Static methods belong to the class, not its objects, and they are stored in the Permanent Generation Space of the heap. Their local variables and arguments are stored in the stack. They can be called without creating an instance of the class, using ClassName.methodName(args).

Important Points:

  • Static methods are shared among all objects of the class.
  • They cannot be overridden as they use static binding at compile time.
  • If both superclass and subclass have static methods with the same name, it is called Method Hiding, where the subclass method hides the superclass method.

Example:

Java
// Java program to demonstrate the static method  
import java.io.*;

class Geeks {
  
  // static method 
  public static void greet(){
    
    System.out.println("Hello Geek!");
  }
    public static void main (String[] args) {
      
       // calling the method directily
       greet();
      
        // calling the method 
        // using the class name
        Geeks.greet();  
    }
}

Output
Hello Geek!
Hello Geek!

Explanation: The above example shows a static method greet() inside the Geeks class, static methods can be called without creating an object. In the main method, we are not creating an object of Geek class we are calling the method directly by the class name which is Geeks and then we are printing the output.

Instance Method

Instance methods are the methods that require an object to work. We need to create an object of the class where the method is written, then only we can access the instance method.

Memory Allocation of Instance Method

Instance methods are stored in the Permanent Generation space of the heap (till Java 7, replaced by Metaspace from Java 8 for better efficiency). Their parameters, local variables, and return values are allocated on the stack. They can be called within their class or from other classes, based on their access modifiers.

Important Points:

  • Instance methods belong to the object, not the class, and require an object to be called.
  • They are stored in one memory location and identify their object through the this pointer.
  • They can be overridden as they use dynamic binding at runtime.

Example:

Java
// Java program to demonstrate the use of instance method
import java.io.*;

class Test {
    String n = "";

    // Instance method 
    public void test(String n) { 
      this.n = n; 
    }
}

class Geeks {
    public static void main(String[] args) {

        // create an instance of the class
        Test t = new Test();

        // calling an instance method 
        // in the class 'Geeks'
        t.test("GeeksforGeeks");
        System.out.println(t.n);
    }
}

Output
GeeksforGeeks

Explanation: The above example shows how to use an instance method in Java. We are creating an object of the Test class and calling the test method to set a value and then we are printing the output.

Note: Instance method requires an object to be called.


Next Article
Article Tags :
Practice Tags :

Similar Reads