← Back to Home

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.

Static Method Examples (Interview-Focused)

1. Basic Static Method Call

class Demo {
    static void greet() {
        System.out.println("Hello Java");
    }

    public static void main(String[] args) {
        greet();
    }
}
          

Explanation: Called without object. Belongs to class.

Output: Hello Java

2. Calling Static Method Using Class Name

class Demo {
    static void show() {
        System.out.println("Static Method");
    }

    public static void main(String[] args) {
        Demo.show();
    }
}
          

Explanation: Recommended syntax.

Output: Static Method

3. Static Method with Parameters

class Demo {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(3, 7));
    }
}
          

Explanation: Parameters behave normally.

Output: 10

4. Static Method Returning Value

class Demo {
    static String tool() {
        return "Selenium";
    }

    public static void main(String[] args) {
        System.out.println(tool());
    }
}
          

Explanation: Can return any type.

Output: Selenium

5. Static Method Accessing Static Variable

class Demo {
    static int x = 10;

    static void show() {
        System.out.println(x);
    }

    public static void main(String[] args) {
        show();
    }
}
          

Explanation: Static method can access static data directly.

Output: 10

6. Static Method Cannot Access Non-Static Variable

class Demo {
    int x = 10;

    static void show() {
        // System.out.println(x); // Compile-time error
    }
}
          

Explanation: Static context cannot access instance members directly.

7. Accessing Non-Static Members from Static Method (Using Object)

class Demo {
    int x = 10;

    static void show() {
        Demo d = new Demo();
        System.out.println(d.x);
    }

    public static void main(String[] args) {
        show();
    }
}
          

Explanation: Object required.

Output: 10

8. Static Method and this Keyword (❌ Not Allowed)

class Demo {
    static void test() {
        // this.x = 10; // Compile-time error
    }
}
          

Explanation: this refers to object. Static methods have no object context.

9. Static Method Overloading

class Demo {
    static void show() {
        System.out.println("No args");
    }

    static void show(int a) {
        System.out.println("With arg");
    }

    public static void main(String[] args) {
        show();
        show(5);
    }
}
          

Explanation: Static methods can be overloaded.

Output:

No args
With arg
          

10. Static Method Hiding (Not Overriding)

class A {
    static void show() {
        System.out.println("A");
    }
}

class B extends A {
    static void show() {
        System.out.println("B");
    }

    public static void main(String[] args) {
        A.show();
        B.show();
    }
}
          

Explanation: Static methods are hidden, not overridden.

Output:

A
B
          

11. Calling Static Method via Object (Allowed but Discouraged)

class Demo {
    static void test() {
        System.out.println("Static");
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.test();
    }
}
          

Explanation: Compiles. Bad practice.

Output: Static

12. Static Method Inside Utility Class

class MathUtil {
    static int square(int x) {
        return x * x;
    }
}

class Demo {
    public static void main(String[] args) {
        System.out.println(MathUtil.square(4));
    }
}
          

Explanation: Common real-world use.

Output: 16

13. Static Method and Static Block

class Demo {
    static {
        System.out.println("Static Block");
    }

    static void show() {
        System.out.println("Static Method");
    }

    public static void main(String[] args) {
        show();
    }
}
          

Explanation: Static block runs first.

Output:

Static Block
Static Method
          

14. Static Method with Varargs

class Demo {
    static void sum(int... nums) {
        int total = 0;
        for (int n : nums) total += n;
        System.out.println(total);
    }

    public static void main(String[] args) {
        sum(1, 2, 3);
    }
}
          

Explanation: Varargs supported.

Output: 6

15. Static Method Returning Object

class Demo {
    static StringBuilder getBuilder() {
        return new StringBuilder("Java");
    }

    public static void main(String[] args) {
        System.out.println(getBuilder());
    }
}
          

Explanation: Can return objects.

Output: Java

16. Static Method and Inheritance Call

class A {
    static void show() {
        System.out.println("A");
    }
}

class B extends A {
    public static void main(String[] args) {
        show();
    }
}
          

Explanation: Static methods are inherited.

Output: A

17. Static Method as Entry Point

class Demo {
    public static void main(String[] args) {
        System.out.println("Entry Point");
    }
}
          

Explanation: JVM starts execution from static main.

Output: Entry Point

18. Static Method vs Non-Static Method

class Demo {
    static void s() {
        System.out.println("Static");
    }

    void ns() {
        System.out.println("Non-static");
    }

    public static void main(String[] args) {
        s();
        new Demo().ns();
    }
}
          

Explanation: Static → class. Non-static → object.

Output:

Static
Non-static
          

19. Static Method and Thread Safety (Stateless)

class Demo {
    static int add(int a, int b) {
        return a + b;
    }
}
          

Explanation: Stateless static methods are thread-safe. Common interview point.

20. Interview Summary – Static Methods

class Demo {
    static int calc(int x) {
        return x * 2;
    }

    public static void main(String[] args) {
        System.out.println(calc(5));
    }
}
          

Explanation: Belongs to class. No object required.

Output: 10