Sub topic 2
Static Nested Classes in Java
A static nested class is a class declared inside another class with the static keyword. It belongs to the enclosing class as a type, but it does not belong to any particular object of that class.
This chapter assumes you can write and compile basic Java classes, understand access modifiers and static members, and use interfaces, abstract classes, and generics.
Why Use a Static Nested Class?
Use a static nested class when a helper type is strongly related to its enclosing class but does not need an enclosing object.
For example, a Pizza.Builder belongs conceptually to Pizza. The builder does not need to read or change one particular Pizza object while it is building it.
A static nested class differs from an inner class in one important way:
| Class kind | Needs an enclosing object? | Example creation |
|---|---|---|
| Static nested class | No | `new Pizza.Builder()` |
| Inner class | Yes | `pizza.new Topping()` |
Because it has no hidden enclosing-object reference, a static nested class is often easier to create, test, and store.
Declaring and Creating One
The following Builder class is nested inside Pizza and is static:
public class Pizza {
private final String size;
private final boolean cheese;
private Pizza(Builder b) {
this.size = b.size;
this.cheese = b.cheese;
}
public static class Builder {
private String size = "medium";
private boolean cheese;
public Builder size(String size) {
this.size = size;
return this;
}
public Builder cheese(boolean cheese) {
this.cheese = cheese;
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
@Override
public String toString() {
return size + (cheese ? " with cheese" : "");
}
public static void main(String[] args) {
Pizza p = new Pizza.Builder()
.size("large")
.cheese(true)
.build();
System.out.println(p); // large with cheese
}
}The expression new Pizza.Builder() names the nested type through Pizza. No Pizza object appears before new.
A static nested class can have instance fields and methods just like a top-level class. The static keyword applies to the relationship with the enclosing class, not to every member inside the nested class.
Accessing Enclosing Members
Nested classes can access private members of their enclosing top-level class. In the example, Builder reads the private constructor of Pizza, and Pizza reads the builder's private fields.
This access does not mean that Builder has a Pizza.this object. A static nested class cannot use Pizza.this, because no enclosing Pizza instance exists.
A static nested class can directly use enclosing static members:
public class Config {
private static final String DEFAULT_FORMAT = "text";
public static class Reader {
public String format() {
return DEFAULT_FORMAT;
}
}
}It cannot directly use an enclosing instance field or instance method. To do that, it must receive an object explicitly:
public class Report {
private String title = "Summary";
public static class Formatter {
public String format(Report report) {
return report.title;
}
}
}When It Is the Right Choice
Choose a static nested class when:
- The type is useful only in the context of the enclosing type.
- The type does not need an enclosing instance.
- You want to group related code without exposing a separate top-level type.
- You want to avoid retaining an outer object through a hidden reference.
A builder, result type, key type, or private implementation helper often fits this pattern.
Exercise
Which declaration correctly creates a Builder nested statically inside Pizza, and why does it not require a Pizza object?
Show answer
public static class Builder { } is correct. A static nested class belongs to the Pizza type rather than to a particular Pizza instance, so it can be created with new Pizza.Builder().