Inner Classes in Java — Static, Non-Static, Local, and Anonymous
Fast recall
Quick revision
Core answer
OuterClass.this — accesses outer private members, memory leak if outer lives shorter than inner (Android adapters, Swing listeners). Anonymous: one-shot interface/abstract impl, replaced by lambdas for functional interfaces. Local: defined in a method, captures effectively-final variables. Static nested is preferred when outer reference isn't needed.Whether the candidate understands the four types of nested classes, their access to the enclosing class, and critically — the memory leak risk of non-static inner classes holding a hidden reference to the outer instance
Not knowing that a non-static inner class holds an implicit reference to its enclosing instance — preventing the outer object from being garbage collected. Confusing static nested classes (which don't need an outer instance) with inner classes (which do)
Explain the memory leak scenario with non-static inner classes in Android or long-lived listeners. Know that anonymous classes have been largely replaced by lambdas since Java 8, but lambdas only work for functional interfaces — anonymous classes are still needed for abstract classes or multi-method interfaces
Interview-ready guide
Interview answer
Java has four types of nested classes. The critical distinction is whether the nested class holds a reference to the enclosing instance.
Non-static inner class — holds `Outer.this` implicitly
- You can't create one without an outer object:
outer.new Inner() - The inner keeps the outer alive in the heap — classic memory leak in Android adapters and event listener lists
- Use only when you genuinely need access to the outer instance's state
Static nested class — no outer reference
- Just a class inside another for namespace organization
- Can access outer's private static members but nothing instance-level
- Default choice — safer, no hidden reference, no leak risk
Local and anonymous classes
- Declared inside a method body; capture local variables only if effectively final (Java copies the value at creation time)
- Since Java 8, lambdas replace most anonymous class usage for functional interfaces
- Anonymous classes are still needed for abstract classes and multi-method interfaces (lambdas require exactly one abstract method)
I recommend defaulting to static nested classes. Reach for non-static inner classes only when the outer instance fields are truly needed.
Inner Classes in Java — Static, Non-Static, Local, and Anonymous
Direct Answer
Static nested: no outer reference, pure namespace. Non-static inner: holds implicit OuterClass.this — accesses outer private members, memory leak if outer lives shorter than inner (Android adapters, Swing listeners). Anonymous: one-shot interface/abstract impl, replaced by lambdas for functional interfaces. Local: defined in a method, captures effectively-final variables. Static nested is preferred when outer reference isn't needed.
Common Mistakes & Pitfalls
Not knowing that a non-static inner class holds an implicit reference to its enclosing instance — preventing the outer object from being garbage collected. Confusing static nested classes (which don't need an outer instance) with inner classes (which do)
Check your understanding
Answer these without looking back at the article.
- Why does a non-static inner class hold a reference to the outer class — when does this cause a memory leak?
- What is the difference between a static nested class and moving it to a top-level class?
- When would you choose an anonymous class over a lambda expression in modern Java?
- How do you access the outer class instance from inside a non-static inner class?
- Why must variables captured by anonymous or local classes be effectively final?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.