Static Nested Class in Java

Naveen AnamaneniUpdated · 5 min read

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

Shop.java▶ Try it
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());
    }
}
Output
Advitha pays Rs. 190

How it works

  1. Line is static, so new Order.Line("Pen", 3, 20) works before any Order exists.
  2. The name Order.Line says what it is: a line of an order. Grouping keeps related types together without cluttering the package.
  3. Order and Line can use each other's private members, because they're nested.
  4. A Line has no link to any particular Order, so it can't read customer on its own.

Static Nested vs Inner

Static nested: no outer reference. Inner: always one
Static nested classMember inner class
Declared withstatic class Lineclass Dashboard
Needs an outer object to be createdNo: new Order.Line()Yes: car.new Dashboard()
Hidden reference to an outer objectNoYes (this$0)
Can read outer instance fields directlyNoYes
Can read outer static members and private members through an objectYesYes
MemoryLighterSlightly heavier, and keeps the outer alive

Trying to read an instance field from a static nested class fails:

StaticNoField.java
public class StaticNoField {
    private int speed = 5;
    static class Nested {
        int read() { return speed; }
    }
}
Compiler output
StaticNoField.java:4: error: non-static variable speed cannot be referenced from a static context
        int read() { return speed; }
                            ^
1 error

Where You'll See Them

ExampleWhy 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.NodeEach node needs data and links, not the list object
Grouped constants or helper typesKeeps 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

  1. Easy: Add a static nested class Address to a Student class, and create an address without creating a student.
  2. Harder: Build a tiny linked list IntList with a private static nested Node class, plus add(int) and sum() methods.

Quick check: Static nested class

4 questions
1How do you create a static nested Line from outside Order?
2Does a static nested object hold a reference to an outer object?
3Can a static nested class read the outer class's instance fields directly?
4Which is a static nested type in the JDK?

Key Takeaways

Was this tutorial helpful?

Lesson 172 of 482