BackendBench
Coffee

Unravel › Java concepts › OOP and types

Java language

Nested and inner classes

A class declared inside another class.

Learn these first

At a glance

Mental model

A static nested class is a tool kept in the same toolbox; an inner class is a tool that is permanently tied to one specific workbench and can reach everything on it.

What happens, in order
  1. 1static class Node inside LinkedList: it can be created on its own (new LinkedList.Node()), and cannot touch the outer instance fields
  2. 2A non-static inner class can read and write the outer object's fields directly, and needs an outer instance to be created: outer.new Inner() or new Inner() from inside the outer class
  3. 3Use nested classes for helpers nobody else needs: a Node for a data structure, a Builder, an EntryInterfaces, enums and records declared inside a class are implicitly static.
Remember
Watch out
Making a helper class inner when it never uses the outer object, causing a hidden memory leakTrying new Inner() from a static method (there is no outer instance)Shadowing outer fields with inner fields of the same name
Example (highlighted lines explain it)
public class Playlist {  private final java.util.List<Song> songs = new java.util.ArrayList<>();  static class Song {    final String title;    Song(String title) { this.title = title; }  }  class Player {    int index;    String current() { return songs.get(index).title; }  }}
Read the full explanation (about 1 min)

A class declared inside another class. A static nested class is just a neighbour with a longer name; an inner (non-static) class also carries a hidden reference to the object that created it.

A mental model

A static nested class is a tool kept in the same toolbox; an inner class is a tool that is permanently tied to one specific workbench and can reach everything on it.

How it works

static class Node inside LinkedList: it can be created on its own (new LinkedList.Node()), and cannot touch the outer instance fields.

A non-static inner class can read and write the outer object's fields directly, and needs an outer instance to be created: outer.new Inner() or new Inner() from inside the outer class.

Use nested classes for helpers nobody else needs: a Node for a data structure, a Builder, an Entry. Interfaces, enums and records declared inside a class are implicitly static.

Going deeper

Prefer static nested classes. An inner class keeps its outer object alive: store it in a cache or pass it to another thread and the whole outer object (and everything it references) cannot be garbage collected.

Inner classes of generic or serializable classes have surprising serialization behaviour because of the hidden outer reference.

Under the hood

The compiler produces separate class files (Outer$Inner.class) and adds a synthetic field this$0 plus a constructor parameter for the outer instance. Since Java 11 private members are shared through nest mates instead of generated accessor methods.

Common mistakes

  • Making a helper class inner when it never uses the outer object, causing a hidden memory leak.
  • Trying new Inner() from a static method (there is no outer instance).
  • Shadowing outer fields with inner fields of the same name.

How Unravel recognises it

When you paste code, Unravel looks for these signs of nested and inner classes. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Nested and inner classes?

A class declared inside another class. A static nested class is just a neighbour with a longer name; an inner (non-static) class also carries a hidden reference to the object that created it.

What are the most common mistakes with Nested and inner classes?

Making a helper class inner when it never uses the outer object, causing a hidden memory leak. Trying new Inner() from a static method (there is no outer instance). Shadowing outer fields with inner fields of the same name.

How can I see Nested and inner classes in real code?

Paste your own Java into Unravel and it will point out where nested and inner classes appears, explain how it works at the detail level you choose, and draw the flow of the code around it.