Static Modifier

Static Modifier

Static is a modifier applicable for methods and variables but not for classes. We cannot declare top-level classes as static, but we can declare inner classes as static (these are called static nested classes).

  • For instance variables, a separate copy is created for every object. For static variables, only one copy is created at the class level and shared by all objects.

Example:

class Test {
    static int x = 10;
    int y = 20;

    public static void main(String[] args) {
        Test t = new Test();
        t.x = 222;
        t.y = 999;

        Test t1 = new Test();
        System.out.print(t1.x + "   " + t1.y);
    }
}

Output: 222 20

  • We cannot access instance members directly from a static area, but we can access them from an instance area.

Example:

class Test {
    int x = 10;

    public void m() {
        System.out.print(x); // Allowed (instance area)
    }

    public static void main(String[] args) {
        System.out.print(x); // Error
    }
}
  • Static members can be accessed directly from both static and instance areas.

Example:

class Test {
    static int x = 10;

    public void m() {
        System.out.print(x);
    }

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

Valid Declarations

  1. int x = 10;
  2. static int x = 10;
  3. public void m() { System.out.print(x); }
  4. public static void m() { System.out.print(x); }

Results:

  • (1 & 3) ✔ Valid
  • (1 & 4) ✖ Error: non-static variable in static context
  • (2 & 3) ✔ Valid
  • (2 & 4) ✔ Valid
  • (1 & 2) ✖ Duplicate variable
  • (3 & 4) ✖ Duplicate method

Case 1: Method Overloading

Overloading is allowed for static methods (including main), but JVM always calls: main(String[] args)

class Test {
    public static void main(int[] args) {
        System.out.print("int[]");
    }

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

Output: String[]

Case 2: Inheritance

class Test {
    public static void main(String[] args) {
        System.out.print("parent method");
    }
}

class P extends Test {
}

java Test → parent method
java P → parent method

Case 3: Method Hiding

Static methods are not overridden; they are hidden.

class Test {
    public static void main(String[] args) {
        System.out.print("parent method");
    }
}

class P extends Test {
    public static void main(String[] args) {
        System.out.print("child method");
    }
}

java Test → parent method
java P → child method

Case 4: Static vs Instance Methods

If a method uses instance variables → Instance Method
If not → Static Method

class Test {
    int rollNo;
    int marks;
    String name;
    static String collegeName;

    String getStudentInfo() {
        return name + " " + marks;
    }

    static String getCollegeName() {
        return collegeName;
    }

    static int getAverage(int x, int y) {
        return (x + y) / 2;
    }

    String getCompleteInfo() {
        return rollNo + " " + marks + " " + name + " " + collegeName;
    }
}

Case 5: Invalid Combination

Static methods must have implementation, but abstract methods do not. So, abstract + static is illegal.

0 Comments