Static Method vs Instance Method in Java
In Java, methods are mainly divided into two parts based on how they are associated with a class, which are the static method and the Instance method. The main difference between static and instance methods is:
- Static method: This method belongs to the class and can be called without creating an object.
- Instance method: This method belongs to an object and requires an object to be called.
Static Method vs Instance Method
The following table lists the major differences between the static methods and the instance method 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. |
| Cannot use the | Can use the |
Override | Does not support runtime polymorphism | Supports runtime polymorphism |
Static Method
A static method can created using the static keyword. It can be called without creating an object of the class, referenced by the class name itself or 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:
// Demonstration of 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!
Instance Method
Instance methods are the methods that require an object of its class to be created before it can be called. To invoke an instance method, we have to create an Object of the class in which the method is defined.
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:
// Demonstrating the in 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
FAQs
What if static variable refers to an Object?
static int i = 1; static Object obj = new Object();
- Value of i: Stored in Metaspace (PermGen in Java 7 and earlier).
- Reference obj: Stored in Metaspace, but the object it refers to is stored in the heap.
When to use static methods?
Use static methods when:
- The method does not depend on the state of an object.
- Code can be shared across all objects of a class (e.g., utility methods).
Can we call static method directly without creating an object?
Yes, static methods can be called directly if they are in the same class, or using the class name if they are in a different class.
How to decide which method to use static or instance?
- Use static methods for class-level operations or utilities.
- Use instance methods for operations that depend on the state of an object.