What Are Static Nested Classes?
You'll learn: What static nested classes are and how they differ from regular top-level classes.
The Core Idea
A static nested class is a class declared inside another class with the static modifier. Despite living inside another class syntactically, it behaves like an independent, top-level class—it doesn't need an instance of the outer class to exist.
Think of it like an apartment building: a static nested class is a self-contained unit that happens to share an address with the building, but it operates completely independently. It doesn't need the building's landlord (the outer class instance) to function.
Why "Static" Matters
The static keyword means the nested class belongs to the outer class itself, not to any specific instance of that class. This is exactly like static methods or static fields you already know—they exist independently of object instances.
Syntax
public class Outer {
// This is a static nested class
public static class StaticNested {
public void display() {
System.out.println("Inside static nested class");
}
}
}
You can create an instance without needing an Outer object:
Outer.StaticNested nested = new Outer.StaticNested();
nested.display();
Notice you use the outer class name as a namespace (Outer.StaticNested), but you don't need an instance of Outer.
Key Differences from Top-Level Classes
Static nested classes are nearly identical to regular classes, except they're declared inside another class for logical grouping. They can access the outer class's static members directly but cannot access instance members without an explicit reference.
Key Takeaway: Static nested classes are independent classes declared inside another class for organizational purposes—they don't need outer class instances to exist.