Sub topic 1
Why Nest Classes, and the Four Kinds
Prerequisites
This chapter assumes you can write and compile basic Java classes, use access modifiers, and distinguish instance members from static members. You should also know interfaces, abstract classes, method overriding, generics, and basic Runnable and Comparator usage.
A nested class is a class declared inside another class or interface. Nesting groups a helper type with the type that uses it. It can communicate design intent, limit visibility, and give the nested code convenient access to related state.
Java has four common kinds:
| Kind | Declared where | Connected to an outer instance? | Typical use |
|---|---|---|---|
| Static nested class | Inside another class with `static` | No | A helper or value type belonging to the outer type |
| Inner member class | Inside another class without `static` | Yes | Behavior tied to one outer object |
| Local class | Inside a method or block | Usually yes | A one-method-scope implementation with a name |
| Anonymous class | In an expression, without a class name | Depends on context | A one-off implementation or subclass |
The Four Kinds
A static nested class belongs to the enclosing class itself. It does not need an enclosing object, so it can be created from a static context:
public class Pizza {
private final String size;
private Pizza(String size) {
this.size = size;
}
public static class Builder {
private String size = "medium";
public Builder size(String size) {
this.size = size;
return this;
}
public Pizza build() {
return new Pizza(size);
}
}
@Override
public String toString() {
return size;
}
public static void main(String[] args) {
Pizza pizza = new Pizza.Builder().size("large").build();
System.out.println(pizza);
}
}Builder is nested for organization and encapsulation. It does not secretly store a Pizza reference.
An inner member class is a non-static class member. Each instance belongs to one instance of the outer class. It can directly use the outer object's instance fields and methods. The connection is part of the object's design, not merely a naming relationship.
A local class is declared inside a method or another block. Its name is available only in that block. Use one when a small implementation needs several methods or fields but should not be visible elsewhere.
An anonymous class is a class expression with no class name. It creates one object while extending a class or implementing an interface:
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("Done");
}
};
task.run();The object has an unnamed class. This is useful for a one-off implementation, although a lambda is often clearer when the target is a functional interface.
Choosing a Kind
Choose a static nested class when the nested type needs the outer type's namespace or private access but no particular outer object. Choose an inner class only when every instance must be associated with an outer object. Choose a local class when its scope should end with a method or block. Choose an anonymous class when naming the implementation would add little value.
A common error is using an inner class by habit. Its hidden link to an outer object affects construction, memory use, and testing. If that link is unnecessary, make the class static.
Exercise
Which kind should you choose for a reusable Builder that does not need a particular outer object?
Show answer
A static nested class. It can be created as new Outer.Builder() without an outer instance.