static vs non-static nested class in Java

In this post, we will learn the difference between "static" and "non-static" nested classes in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between "static" and "non-static" nested classes in Java

Feature Static Nested Class Non-Static (Inner) Nested Class
Declaration and Instantiation Declared using the "static" keyword within another class. Declared within another class without the "static" keyword.
Instantiation Can be instantiated without creating an instance of the enclosing class. Requires an instance of the enclosing class to be instantiated.
Access to Enclosing Class Can only access static members of the enclosing class directly. Can access both static and instance members of the enclosing class directly.
Relationship to Enclosing Class Is loosely coupled with the enclosing class. It is like a static member of the enclosing class. Is tightly coupled with the enclosing class. It has a direct reference to the instance of the enclosing class.
Static Members Can only directly access other static members within the static nested class. Can directly access both static and instance members of the enclosing class.
Instance Members Cannot access instance, members of the enclosing class, directly. Can access instance, members of the enclosing class, directly.
Enclosing Class Object Does not hold a reference to the instance of the enclosing class. Holds a reference to the instance of the enclosing class.

Example

Let's create an example to demonstrate the difference between static and non-static (also known as inner) nested classes in Java. 

Static Nested Class: 

A static nested class is a nested class that is declared as static. It does not have access to the instance variables and methods of the outer class directly and can be accessed using the outer class name.
public class OuterClass {
    private static String outerMessage = "Hello from OuterClass (static)";
    
    // Static nested class
    public static class StaticNestedClass {
        public void display() {
            System.out.println("Message from StaticNestedClass: " + outerMessage);
        }
    }
    
    public static void main(String[] args) {
        // Accessing static nested class
        StaticNestedClass nested = new StaticNestedClass();
        nested.display();
    }
}

Output:

Message from StaticNestedClass: Hello from OuterClass (static)

Non-Static Nested class

A non-static nested class, also known as an inner class, is declared without the static keyword. It has access to the instance variables and methods of the outer class and can be instantiated only through an instance of the outer class.
public class OuterClass {
    private String outerMessage = "Hello from OuterClass (non-static)";
    
    // Inner (non-static nested) class
    public class InnerClass {
        public void display() {
            System.out.println("Message from InnerClass: " + outerMessage);
        }
    }
    
    public static void main(String[] args) {
        // Creating an instance of OuterClass
        OuterClass outer = new OuterClass();
        
        // Accessing inner class
        InnerClass inner = outer.new InnerClass();
        inner.display();
    }
}

Output:

Message from InnerClass: Hello from OuterClass (non-static)

Explanation: 

In the example of the static nested class, we access the nested class directly using the outer class name, and it doesn't require an instance of the outer class to be created. 

In the example of the non-static (inner) nested class, we first create an instance of the outer class OuterClass, and then we use that instance to create an instance of the inner class InnerClass. The inner class has access to the instance variables and methods of the outer class, which is demonstrated by displaying the outerMessage from the inner class.


Comments