Java Reference ยท Object-Oriented Concepts
Static nested classes in Java
Reference for Java static nested classes: declaration, instantiation, access to outer private members, differences from inner classes, and when to use them.
A static nested class is a class declared with static inside another class. It is a normal class in every respect except its name and access: it lives in the namespace of the outer class (Outer.Nested) and can read the outer class's private static members, and the outer class can read its private members. It has no hidden reference to an instance of the outer class.
That last point separates it from an inner class, which holds an implicit Outer.this. Prefer a static nested class whenever the nested type does not need the enclosing instance. It uses less memory, cannot cause accidental leaks of the outer object, and can be created on its own.
class Outer { static class Nested { } } Outer.Nested n = new Outer.Nested();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
[access] static class Name [extends Super] [implements Iface] { ... }
| Feature | Static nested class |
|---|---|
| Modifiers | public, protected, package-private, private, plus final, abstract, sealed. |
| Outer instance | None. Create it with new Outer.Nested(). |
| Access to outer | Static members of the outer class, including private ones. Instance members only through an explicit reference. |
| Members | May declare any members, static or not. |
| Generic parameters | Its own; it does not share the outer class's type parameters. |
| Interfaces | Nested types declared in an interface are implicitly public static. |
Usage notes
- Records, enums and interfaces declared as members are implicitly static, so a nested
record Point(int x, int y)is a static nested type already. - Use
private staticfor helpers such as linked-list nodes, map entries and iterators that only the outer class sees. This keeps the public API small. - Builders are commonly static nested classes:
Pizza.Builder. - Names: the binary name is
Outer$Nested.getSimpleName()returnsNested. - Import: import with
import pkg.Outer.Nested;to refer to it asNested. - Access works both ways: the outer class can use a nested class's private fields and constructors, even though they look hidden.
- Memory leaks: an inner (non-static) class keeps its outer instance alive; if you do not need that, add
static. - Testing: because they are ordinary classes, they can be unit-tested if not private.
- Instance-less utility groupings (constants, helper classes) are fine, but separate top-level files are often clearer once a class grows.