static Methods
A static method in Java belongs to the class itself, not to any specific object of the class. It can be called without creating an object, making it ideal for utility, helper, and common functionality. This is a highly important interview topic, especially when comparing static vs non-static methods.
What Is a static Method?
- Declared using the static keyword
- Belongs to the class, not to objects
- Can be called using ClassName.methodName()
- Loaded into memory once, at class loading time
static void display() {
System.out.println("Static method");
}
Why Use static Methods?
- No object creation required
- Memory efficient
- Suitable for common logic
- Used for utility/helper functions
Example: Math.max(), Math.sqrt(), Collections.sort()
Syntax of a Static Method
accessModifier static returnType methodName(parameters) {
// method body
}
Calling a static Method
1. Using Class Name (Recommended)
MyClass.show();
2. Using Object (Not Recommended)
MyClass obj = new MyClass();
obj.show(); // Works, but discouraged
Example Program
class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = Calculator.add(10, 20);
System.out.println(result);
}
}
Key Rules of static Methods (Very Important)
1. Can Access Only Static Members Directly
static int x = 10;
int y = 20;
static void show() {
System.out.println(x); // OK
// System.out.println(y); // Compile-time error
}
2. Cannot Use this or super
static void test() {
// this.x = 10; // ❌ Not allowed
}
Reason: this refers to an object, and static methods are object-independent.
3. Static Methods Can Be Overloaded
static void show() { }
static void show(int a) { }
✔ Valid
4. Static Methods Cannot Be Overridden (Method Hiding)
Static methods are hidden, not overridden.
class A {
static void show() {
System.out.println("A");
}
}
class B extends A {
static void show() {
System.out.println("B");
}
}
Method call depends on reference type, not object.
Static Method vs Instance Method
| Feature | Static Method | Instance Method |
|---|---|---|
| Belongs To | Class | Object |
| Object Required | No | Yes |
| Access Non-static | ❌ No | ✅ Yes |
| Uses this | ❌ No | ✅ Yes |
| Memory | Single copy | Per object |
| Overriding | ❌ No | ✅ Yes |
main() Method as a Static Method
public static void main(String[] args)
Why static?
- JVM must call it without creating an object
When to Use static Methods
- Utility/helper methods
- Stateless logic
- Common validation logic
- Factory/helper functions
When NOT to Use static Methods
- When behavior depends on object state
- When polymorphism is required
- When overriding behavior is needed
Common Beginner Mistakes
- Accessing non-static variables directly
- Using this in static context
- Calling static methods using objects
- Confusing static method hiding with overriding
Interview-Ready Answers
Short Answer
A static method belongs to the class and can be called without creating an object.
Detailed Answer
In Java, static methods are associated with the class rather than instances. They can access only static members directly and are commonly used for utility or helper functionality. Static methods cannot be overridden but can be overloaded.
Key Takeaway
Use static methods for class-level, stateless behavior. Avoid them when object-specific behavior or polymorphism is required.