Sub topic 11
Static Nested Classes
What a static nested class is
This chapter assumes you know how to define classes, create objects, use access modifiers, and override methods.
A nested class is a class declared inside another class. A static nested class is a nested class declared with static:
class Outer {
static class Nested {
}
}The nested class belongs to the outer class's namespace, but it does not belong to any particular outer object. Create it with the outer class name:
Outer.Nested value = new Outer.Nested();This is different from a non-static nested class, also called an inner class:
class Outer {
class Inner {
}
}
Outer outer = new Outer();
Outer.Inner value = outer.new Inner();An inner class requires an outer object. A static nested class does not. It has no hidden reference to an outer object, which can reduce memory use and make its relationship with the outer class clearer.
Accessing outer class members
A static nested class is a static context. It can access the outer class's static members directly. It cannot access an outer instance field without receiving an outer object.
It can access private members because both classes are part of the same enclosing type. However, an instance member still needs an explicit object reference.
public final class Computer {
private static final String DEFAULT_BRAND = "Nova";
private static int computersBuilt;
private final String model;
private Computer(String model) {
this.model = model;
computersBuilt++;
}
public static int builtCount() {
return computersBuilt;
}
public static final class Specification {
private final int memoryGb;
private final boolean portable;
public Specification(int memoryGb, boolean portable) {
if (memoryGb <= 0) {
throw new IllegalArgumentException("Memory must be positive");
}
this.memoryGb = memoryGb;
this.portable = portable;
}
public Computer build(String model) {
String fullModel = DEFAULT_BRAND + " " + model;
return new Computer(fullModel);
}
public String describe() {
String type = portable ? "portable" : "desktop";
return memoryGb + " GB " + type;
}
}
public static void main(String[] args) {
Computer.Specification specification =
new Computer.Specification(16, true);
Computer computer = specification.build("A1");
System.out.println(computer.model);
System.out.println(specification.describe());
System.out.println(Computer.builtCount());
}
}The output is:
Nova A1
16 GB portable
1Specification can read DEFAULT_BRAND, which is static. It can also call the private Computer constructor. The constructor's model field belongs to each Computer object, so Specification would need a Computer reference to read it.
Why use one?
A static nested class is useful when a type has a strong relationship with its enclosing class but does not need an enclosing object.
Common examples include:
- A
Builderused to construct the outer type. - A
SpecificationorOptionsobject that configures the outer type. - A private helper type used only by one implementation.
- A result type that is meaningful only in the outer class's API.
A builder often uses this design:
flowchart TD
C["Computer"] --> S["Computer.Specification"]
S --> B["build(model)"]
B --> O["Computer object"]The nested type is grouped with Computer, while each Specification object remains independent of any existing Computer.
Static nested versus inner classes
| Property | Static nested class | Inner class |
|---|---|---|
| Requires an outer object | No | Yes |
| Created with | `new Outer.Nested()` | `outer.new Inner()` |
| Implicit outer reference | No | Yes |
| Direct access to outer instance fields | No | Yes |
| Best for | Independent related types | Behavior tied to one outer object |
Choose an inner class when each nested object must work with one specific outer object. Choose a static nested class when passing required data explicitly is clearer.
Exercise
What is the main difference between Outer.Nested and Outer.Inner?
Show answer
A static nested class such as Outer.Nested does not require an outer object. An inner class such as Outer.Inner does require one and has an implicit reference to it.