A static nested class is a nested class that is declared with the static modifier. It differs from a regular (non-static) inner class in several important ways, mainly in how they access members of the enclosing class.

Static Nested Class

  • Definition: A static nested class is a class defined within another class, but it does not require an instance of the outer class to be instantiated. It can access only the static members of the outer class.
  • Usage: It is typically used when the nested class is logically associated with the outer class but does not need to access instance-specific members or methods of the outer class.

    class Outer {
        private static int staticVar = 10;
          
        static class Nested {
            void display() {
                System.out.println("Static var: " + staticVar); // Can access static member
            }
        }
    }
    

Inner Class (Non-static Nested Class)

  • Definition: An inner class is a non-static nested class. It has access to all members (both static and instance) of the outer class and requires an instance of the outer class to be created before it can be instantiated.
  • Usage: It is typically used when the nested class needs to access instance-specific data or methods of the outer class.

    class Outer {
        private int instanceVar = 20;
          
        class Inner {
            void display() {
                System.out.println("Instance var: " + instanceVar); // Can access instance member
            }
        }
    }
    

Key Differences

  1. Instance Requirement:
    • A static nested class does not need an instance of the outer class to be instantiated.
    • An inner class requires an instance of the outer class to be created.
  2. Access to Outer Class Members:
    • A static nested class can only access static members of the outer class.
    • An inner class can access both static and instance members of the outer class.
  3. Memory Considerations:
    • A static nested class behaves like a top-level class and does not hold an implicit reference to the outer class instance.
    • An inner class holds an implicit reference to an instance of the outer class, which can increase memory usage.

Example of Static Nested Class

class Outer {
    static int staticVar = 100;
    
    static class Nested {
        void show() {
            System.out.println("Static variable: " + staticVar);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Nested nested = new Outer.Nested();
        nested.show();  // No need for an instance of Outer class
    }
}

Example of Inner Class

class Outer {
    int instanceVar = 50;
    
    class Inner {
        void show() {
            System.out.println("Instance variable: " + instanceVar);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();  // Need an instance of Outer to create Inner
        inner.show();
    }
}

Conclusion

  • Static nested class: Does not require an outer class instance and can only access static members.
  • Inner class: Requires an instance of the outer class and can access both static and instance members.