Static Nested Class in Java
A static nested class is declared inside another class with the keyword static. Despite living inside, it behaves like an ordinary top-level class that happens to be grouped with its outer class. It needs no outer object, carries no hidden reference, and can be created anywhere as new Outer.Nested(...). It's the most common kind of nested class, and the right default.
An Order and Its Lines
import java.util.ArrayList;
import java.util.List;
public class Shop {
static class Order {
private final String customer;
private final List<Line> lines = new ArrayList<>();
Order(String customer) { this.customer = customer; }
static class Line {
final String item;
final int qty;
final long price;
Line(String item, int qty, long price) {
this.item = item;
this.qty = qty;
this.price = price;
}
long total() { return qty * price; }
}
void add(Line line) { lines.add(line); }
long total() {
long sum = 0;
for (Line l : lines) {
sum += l.total();
}
return sum;
}
}
public static void main(String[] args) {
Order.Line pens = new Order.Line("Pen", 3, 20);
Order.Line books = new Order.Line("Notebook", 2, 65);
Order order = new Order("Advitha");
order.add(pens);
order.add(books);
System.out.println(order.customer + " pays Rs. " + order.total());
}
}Advitha pays Rs. 190How it works
Lineisstatic, sonew Order.Line("Pen", 3, 20)works before anyOrderexists.- The name
Order.Linesays what it is: a line of an order. Grouping keeps related types together without cluttering the package. OrderandLinecan use each other's private members, because they're nested.- A
Linehas no link to any particularOrder, so it can't readcustomeron its own.
Static Nested vs Inner
| Static nested class | Member inner class | |
|---|---|---|
| Declared with | static class Line | class Dashboard |
| Needs an outer object to be created | No: new Order.Line() | Yes: car.new Dashboard() |
| Hidden reference to an outer object | No | Yes (this$0) |
| Can read outer instance fields directly | No | Yes |
| Can read outer static members and private members through an object | Yes | Yes |
| Memory | Lighter | Slightly heavier, and keeps the outer alive |
Trying to read an instance field from a static nested class fails:
public class StaticNoField {
private int speed = 5;
static class Nested {
int read() { return speed; }
}
}StaticNoField.java:4: error: non-static variable speed cannot be referenced from a static context
int read() { return speed; }
^
1 errorWhere You'll See Them
| Example | Why static nested |
|---|---|
Map.Entry<K, V> | A key-value pair belongs with Map but doesn't need a map object (it's a nested interface, implicitly static) |
Builders: Pizza.Builder (see "Builder Pattern in Java") | Builds the outer object before it exists |
Nodes in a linked list: LinkedList.Node | Each node needs data and links, not the list object |
| Grouped constants or helper types | Keeps the package tidy |
Common Mistakes
Calling it a "static class" in general. Only nested classes can be static; a top-level class can't.
Reading outer instance fields directly. Pass the outer object in, or make the class inner.
Forgetting the outer name from outside. It's Order.Line, or import it: import com.x.Order.Line;.
Interview Corner
What is the difference between a static nested class and an inner class?
A static nested class is declared with static, has no reference to an outer instance, can be instantiated without one (new Outer.Nested()), and can directly access only the outer class's static members. An inner class is non-static: each instance holds a hidden reference to an outer instance, must be created through one (outer.new Inner()), and can access the outer instance's fields directly. Static nested classes are lighter and avoid accidental memory retention.
Can a top-level class be declared static?
No. static applies only to members, so only nested classes can be static. A top-level class can be public, package-private, final, abstract, sealed or non-sealed.
Practice
- Easy: Add a static nested class
Addressto aStudentclass, and create an address without creating a student. - Harder: Build a tiny linked list
IntListwith a private static nestedNodeclass, plusadd(int)andsum()methods.
Key Takeaways
Was this tutorial helpful?