Static Method vs Instance Method in Java
In Java, methods define the behavior of classes and objects. Understanding the difference between static methods and instance methods is essential for writing clean and efficient code.
What is a Static Method?
A static method belongs to the class rather than any specific object.
- Can be called without creating an instance of the class.
- Since static methods are not object-specific, they can access only static members (data and methods), and cannot access non-static members.
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();
// 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 Geeks class we are calling the method directly by the class name which is Geeks and then we are printing the output.
What is an Instance Method?
An Instance method belongs to an object.
- Need to create an instance of the class to call.
- Can access instance variables, other instance methods, and static members of the class.
- Have access to
thisreference, which points to the current object.
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.
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. |
| 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 | Loaded once per class | Each object has its own copy |
Which statement correctly describes a static method in Java?
-
A
It always requires an object to be called
-
B
It belongs to the class, not objects
-
C
It can access all instance variables
-
D
It supports runtime polymorphism
Static methods are class-level methods and do not depend on object creation.
Which of the following can an instance method access?
-
A
Only static data
-
B
Only instance data
-
C
Both static and instance members
-
D
Only inherited members
Instance methods can access all members of the class—static and instance.
How many times is a static method loaded into memory?
-
A
Once per object
-
B
Once per JVM thread
-
C
Once per class
-
D
Every time it is called
Static methods are loaded once when the class is loaded into memory.