Course contents

Inner, static nested and anonymous classes

The four kinds of nested classes and what javac generates for each: the hidden Outer.this reference and leaks, static nested Builders, effectively final capture, anonymous classes versus lambdas, nestmates and serialization pitfalls.

PremiumOften askedLesson16 min readCoreReviewed Oct 2026 · Java 21
On this page
  1. Picture it first: a house, its shed, its rooms and a tent
  2. Under the hood: what javac generates for each kind
  3. Real world: the leak hiding in this$0
  4. Advanced: lambdas, nestmates and serialization
  5. How to say it in the interview

🪆 What are nested classes?

A nested class is a class declared inside another class, method or expression. Java has four kinds: static nested classes, inner classes (non-static members), local classes (declared in a method) and anonymous classes (declared and instantiated in one expression). For example, Order.Builder is a static nested class, while the iterator that ArrayList.iterator() returns is an inner class that reads the list's private array.

Nested classes look like a syntax topic, but interviewers use them to probe something deeper: do you know what the compiler generates behind your back? An inner class carries a hidden reference to its outer object, which is how a 2 KB listener can keep a 200 MB page alive. An anonymous class and a lambda look interchangeable until you print this. And private access between them used to need synthetic bridge methods until Java 11.

What you’ll learn

  • Tell apart static nested, inner, local and anonymous classes, and name the class file each one produces
  • Explain the hidden this$0 field in inner classes and trace the memory leak it causes
  • Say why static nested is the default choice, using the Builder as the example
  • Explain why captured locals must be effectively final (values are copied into synthetic fields)
  • Contrast anonymous classes with lambdas: this, class files, invokedynamic and allocation
  • Describe nestmates (JEP 181, Java 11) and why serializing inner classes is discouraged

For this lesson, we'll go through the four kinds and what javac emits for each, the hidden Outer.this reference and the leaks it causes, why static nested is the right default (the Builder), effectively final capture, anonymous classes versus lambdas, nestmates (JEP 181), and why serializing inner classes is a trap.

Below the line (out of scope)

The 30-second answer

There are four kinds. A static nested class is a normal class that lives in the outer class's namespace and can see its private members, but has no link to an outer instance. An inner class is tied to one outer instance: javac adds a hidden this$0 field and a constructor parameter, so every inner object keeps its outer object reachable. Local classes live inside a method, and anonymous classes are one-off expressions; both can capture locals only if they're effectively final, because the values are copied in. My default is static nested, like a Builder, because the hidden reference costs memory and causes leaks. For a single-method interface I use a lambda instead of an anonymous class: in a lambda this means the enclosing object and no extra class file is generated. Since Java 11 the JVM understands nests, so private access between them needs no bridge methods.

Interview radar

Asked very often: "What's the difference between a static nested class and an inner class?" and "Why must captured variables be effectively final?" Senior rounds probe: the hidden this$0 reference and leaks, this in an anonymous class versus a lambda, and nestmates replacing access$000 bridges. Production angle: a listener or callback registered with a long-lived registry that keeps a whole page, request or Activity in memory.

01Picture it first: a house, its shed, its rooms and a tent

Think of the outer class as a house. A static nested class is the garden shed on the same plot: it shares the address and has a key to the house, but it stands on its own, and you can build a shed without anyone living in the house. An inner class is a room: it can't exist without a specific house, and every room knows exactly which house it's in. A local class is a tent pitched inside one room for one evening (one method call). An anonymous class is the same tent without a name tag, put up once and never referred to by name again.

Here are the four kinds side by side, with what each one costs you:

Fig. 1

A comparison grid of the four nested class kinds: static nested has no outer reference and compiles to Outer$Node, inner always holds Outer.this and compiles to Outer$Itr, local and anonymous classes hold Outer.this only in a non-static context, capture effectively final locals, and compile to Outer$1Step and Outer$1

A comparison grid of the four nested class kinds: static nested has no outer reference and compiles to Outer$Node, inner always holds Outer.this and compiles to Outer$Itr, local and anonymous classes hold Outer.this only in a non-static context, capture effectively final locals, and compile to Outer$1Step and Outer$1

Notice two things. Only static nested classes are guaranteed to be free of an outer reference. And the two kinds declared inside a method get numbered class files, which matters later for serialization.

Premium lesson

Keep reading with the Java Pass

The rest of this lesson is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

  • Under the hood: what javac generates for each kind
  • Real world: the leak hiding in this$0
  • Advanced: lambdas, nestmates and serialization
  • How to say it in the interview

From ₹199 · one-time payment · No auto-renewal

Practise this topicNested types: interview questions & scenarios
Further reading (9)