Inner classes
An inner class is a non-static class declared inside another class. Each instance holds a hidden reference to an enclosing instance and can use its fields directly.
Detailed Explanation
Sometimes a helper type only makes sense in the context of one object. An iterator over a list needs the list. A listener on a button needs the button. An inner class is that helper, nested in the source and tied to the outer instance at runtime.
Java has four nested types. Static nested classes are ordinary classes with a namespaced name; they do not hold an outer instance. Inner classes (the non-static member kind) do. Local classes are declared inside a method. Anonymous classes are local classes without a name. This page is about the member inner class.
The hidden field is why inner classes exist and why they cost. The compiler inserts a reference to the enclosing instance. The inner code can read outerField as if it were local. You can also write Outer.this when names collide. Construction looks like outer.new Inner(). From inside an instance method of Outer, new Inner() is enough; the current this becomes the enclosing instance.
Inner classes can be private, which is the usual choice. The type never appears in a public signature. Callers see Iterator, not ArrayList.Itr. That is encapsulation of implementation types, not only of fields.
You reach for an inner class when the helper must mutate or read the outer object and will not outlive it. You reach for a static nested class when the helper is related in name only (a Builder, a Node that stores its own links). Prefer static. The extra outer reference is a common memory leak: an inner instance kept in a cache pins the entire outer object.
Local and anonymous classes capture enclosing local variables. Those variables must be final or effectively final. The capture is a copy of the reference (or primitive value), not a live binding. Reassigning the local after the class is created is a compile error.
The cost besides memory is syntax and class files. Each nested type becomes a separate .class file (Outer$Inner.class). Public inner classes in an API are awkward to construct and document. Keep them private unless the type is part of the contract.
Code Examples
public class Counter {
private int value;
public Counter(int value) {
this.value = value;
}
// Non-static: each Stepper is tied to one Counter.
public class Stepper {
public void up() {
value++; // reads the enclosing instance field
}
}
public int value() {
return value;
}
public static void main(String[] args) {
Counter c = new Counter(1);
Counter.Stepper s = c.new Stepper();
s.up();
System.out.println(c.value()); // prints: 2
}
}public class Box {
private final String label;
public Box(String label) {
this.label = label;
}
// Static nested: no hidden Box reference.
public static final class Label {
private final String text;
public Label(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
public static void main(String[] args) {
Box.Label l = new Box.Label("fragile");
System.out.println(l); // prints: fragile
System.out.println(new Box("bin").label); // prints: bin
}
}Common Gotchas
An inner instance keeps the outer instance alive
store a static nested class, or a top-level class, if the helper must outlive the outer object.
No enclosing instance in a static method
`new Inner()` inside `main` does not compile unless you write `outer.new Inner()`.
Serializing an inner class also serializes the outer instance
that surprise has leaked passwords and whole object graphs.
Prefer `static` nested for builders and nodes
they rarely need the outer `this` and are cheaper and simpler.