Detailed explanation
In Java, a nested class is a class declared inside another class or interface. For the member classes in this example, the key distinction is whether the declaration uses static. Outer.StaticNested is a static nested class: its objects have no implicitly associated Outer instance. Outer.Inner is a non-static member inner class: each object is associated with an enclosing Outer instance. Both are useful for grouping related implementation details, but an inner class also expresses a relationship with a particular outer object.
Instantiation makes this difference visible. You create the static nested class with Outer.StaticNested sn = new Outer.StaticNested(); no Outer object is needed. For the inner class, first create Outer o = new Outer(); and then use Outer.Inner in = o.new Inner();. Inside an instance method of Outer, new Inner() can use the current Outer object implicitly. The static modifier does not make nested-class objects singletons or make their fields static; each object can still have its own instance state.
A static nested class can directly access static members of Outer, including private ones, but needs an explicit Outer reference to access outer instance members. An inner class can directly access both static members and instance members of its enclosing object, including private members. Outer.this identifies that enclosing object, while this identifies the inner object. Compilers typically implement the enclosing-instance relationship using an additional constructor parameter and, when needed, a synthetic field. Consequently, an inner object that retains its enclosing instance can keep the outer object reachable, which matters when callbacks or listeners outlive their intended owner.
Prefer a static nested class when the helper needs logical grouping but does not depend on a particular outer object's state. Choose an inner class when that association is intentional and useful. Interviewers usually expect correct construction syntax, access rules, and the lifetime implication—not merely the keywords. Also avoid the outdated claim that inner classes cannot declare static members: since Java 16, they can, including static initializers. Earlier Java versions generally restricted them to static constant variables.
Key points to remember
- A static nested object has no implicit enclosing instance; a member inner object does.
- Construct them using new Outer.StaticNested() and outerObject.new Inner(), respectively.
- Static nested classes need an explicit object reference to access outer instance members.
- Inner classes can use Outer.this to refer to their enclosing object.
- Prefer static nesting unless the helper genuinely needs an enclosing-instance relationship.
Common mistakes candidates make
- Assuming static means the nested class has only one object or only static fields.
- Trying to create a member inner object from a static context without supplying an enclosing instance.
- Claiming inner classes can never declare static methods or initializers, without accounting for Java 16 and later.
Likely follow-up questions
Can a static nested class access private members of the outer class?
Yes, nesting permits access to the outer class's private members. However, private instance members still require an explicit Outer object reference; private static members do not.
Does using an inner class always cause a memory leak?
No; the enclosing-instance relationship is usually intentional and harmless. Unwanted retention can occur when a long-lived inner object holds its enclosing instance reachable after that outer object is otherwise no longer needed.
What happens if the inner and outer classes declare fields with the same name?
Inside the inner class, this.field refers to its own field, while Outer.this.field refers to the enclosing object's field. This explicitly resolves the shadowing without creating another Outer object.
Interview tip: Start with the enclosing-instance distinction, show both construction expressions, and finish with access rules and one practical lifetime implication.
