Java Reference ยท Object-Oriented Concepts
Inner and nested classes in Java
The four kinds of Java nested classes: static nested, inner (member), local and anonymous. Syntax, access to outer members, this and memory leaks.
A nested class is a class declared inside another class. Java has four kinds. A static nested class is a member marked static; it behaves like a top-level class that happens to live in the namespace of its enclosing class. An inner class is a member class without static; every instance is tied to an instance of the enclosing class and can use its fields directly. The other two kinds, local classes and anonymous classes, are declared inside method bodies.
Nesting is useful when a class only makes sense in the context of another one: an iterator for a collection, a node of a linked list, a builder for a value type.
class Outer { private int x = 10; static class Nested { } // no outer instance class Inner { // has an outer instance int read() { return x; } // reads Outer.this.x } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Comparison
| Kind | Declared | Outer instance | Typical use |
|---|---|---|---|
| Static nested | member, static |
none | Helper types, builders, nodes, entries |
| Inner (member) | member, not static |
required (Outer.this) |
Iterators and views over one outer object |
| Local | inside a method or block | if the method is an instance method | One-off helper used in a single method |
| Anonymous | expression with new Type() { ... } |
if in an instance context | Small one-off implementations; mostly replaced by lambdas |
Usage notes
- Create inner objects with an outer instance. Inside the outer class,
new Inner()usesthis; elsewhere writeouter.new Inner(). Outer.thisnames the enclosing instance; plainthisis the inner object. A same-named field in the inner class hides the outer one.- Full access both ways. An inner (or nested) class can read the outer class's
privatemembers, and the outer class can read the nested class's private members. - Inner classes keep their outer object alive. Storing an inner instance in a long-lived structure can leak the outer one. Make the class
staticwhenever it does not use the outer instance; this is the usual advice for nodes and listeners. - Members of inner classes: since Java 16 an inner class may declare
staticmembers; earlier versions only allowed constant fields. - Access modifiers apply: nested classes can be
private,protected, package-private orpublic. Private nested classes are an implementation detail the outside never sees. - Binary names contain a
$:Outer$Inner.getSimpleName()gives the short name andgetName()the binary name; the name printed above depends on the file's implicit class when using the source launcher. - Interfaces, enums and records declared as members are implicitly
static.