Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Static nested classes in Java

A static nested class belongs to an enclosing type, but you do not need an instance of that type to create a nested object.

Version: Java SE 21

Syntax

class Outer {
    static class Order { }
    static final class Helper { }
    class Inner { }
}

// In a method or initializer where access is permitted:
Outer.Order order = new Outer.Order();
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();

In Java SE 21, static class declares a static member class. Writing static class Order inside Outer declares the type Outer.Order. If the type and constructor are accessible, use new Outer.Order(...) without creating an Outer object. You cannot write an explicit static modifier on a top-level class or an ordinary local class.

A non-static member class Inner is an inner class. Each instance is associated with a particular Outer object; from outside Outer, you can create it with outer.new Inner() if access rules permit. In Java SE 21, an inner class can declare its own static members. That does not make the class itself static.

A static nested class can use the enclosing class's static members, including private ones. To read an instance field of the enclosing class, it needs an explicit object reference, as in shop.store. It cannot use the type parameter T from Outer<T>, but it can declare its own, as in static class Box<U> {}.

In static final class, static means there is no implicit enclosing instance, while final prevents subclassing. Neither modifier makes objects immutable or limits the class to one instance. The class can have its own instance fields and methods. Use static nesting for a related data model, builder, or helper that does not need an implicit enclosing instance.

Examples

public class Main {
    private static final String PREFIX = "ORD-";
    private final String store;

    private Main(String store) {
        this.store = store;
    }

    static class Order {
        public final String id;

        Order(String id) {
            this.id = id;
        }

        String label() {
            return PREFIX + id;
        }
    }

    static final class Receipt {
        private Receipt() {
        }

        String format(Main shop, Order order) {
            return shop.store + ": " + order.label();
        }
    }

    public static void main(String[] args) {
        Main.Order order = new Main.Order("42");
        Main.Receipt receipt = new Main.Receipt();
        System.out.println(order.label());

        Main shop = new Main("North");
        System.out.println(receipt.format(shop, order));
    }
}

Result

ORD-42
North: ORD-42

`Main` creates an `Order` and a `Receipt` before any `Main` object exists. `Order` reads the private static field `PREFIX`; `Receipt` reads the private field `store` through the explicitly supplied `shop` object. The private `Receipt` constructor is accessible inside `Main`.

Related lessons

  • Java · Lesson 1 · Advanced · Version: 2
  • Java · Lesson 2 · Advanced · Version: 2
  • Java · Lesson 10 · Advanced · Version: 2
  • Java · Lesson 16 · Advanced · Version: 2
  • Java · Lesson 17 · Advanced · Version: 2
  • Java · Lesson 18 · Advanced · Version: 2
  • Java · Lesson 23 · Advanced · Version: 2
  • Java · Lesson 24 · Advanced · Version: 2
  • Java · Lesson 25 · Advanced · Version: 2
  • Java · Lesson 27 · Advanced · Version: 2
  • Java · Lesson 35 · Advanced · Version: 2
  • Java · Lesson 38 · Advanced · Version: 2
  • Java · Lesson 3 · Intermediate · Version: 2
  • Java · Lesson 7 · Intermediate · Version: 2
  • Java · Lesson 12 · Intermediate · Version: 2
  • Java · Lesson 16 · Intermediate · Version: 2
  • Java · Lesson 22 · Intermediate · Version: 2
  • Java · Lesson 26 · Intermediate · Version: 2
  • Java · Lesson 27 · Intermediate · Version: 2
  • Java · Lesson 28 · Intermediate · Version: 2
  • Java · Lesson 30 · Intermediate · Version: 2
  • Java · Lesson 33 · Intermediate · Version: 2
  • Java · Lesson 42 · Intermediate · Version: 2
  • Java · Lesson 52 · Intermediate · Version: 2
  • Java · Lesson 53 · Intermediate · Version: 2
  • Java · Lesson 54 · Intermediate · Version: 2

Sources