skip to content

What is a static nested class in Java, and how do you declare one?

level: juniorimportance: must knowfreq 70%

answer

  1. static keyword on a member class
  2. new Outer.Nested() — no outer instance
  3. namespaced top-level class
  4. Map.Entry, Builder are examples
  5. belongs to the class, not an object

basics

~10 s

A static nested class is a class declared inside another class with the static keyword. You can create and use it without first creating an object of the outer class: new Outer.Nested().

solid answer

~40 s

A static nested class is a class defined as a static member of an enclosing (outer) class. Because it is static, it is associated with the outer class itself, not with any instance of it. That means you can instantiate it directly with `new Outer.Nested()` without an enclosing-class object. It behaves much like a top-level class but is namespaced inside its enclosing class, which signals a strong logical grouping and lets it access the outer class's private static members. You declare it by putting `static` on a class member: `class Outer { static class Nested { } }`. Typical uses are helper types tightly bound to the outer class (builders, comparators, node types for a data structure) where you want clear scoping without a separate file.

code

java · 16 lines
java
public class Outer {
    private static int counter = 0;

    static class Nested {
        void bump() {
            counter++; // OK: can touch outer's static members
        }
    }
}

class Demo {
    public static void main(String[] args) {
        Outer.Nested n = new Outer.Nested(); // no Outer instance needed
        n.bump();
    }
}

go deeper

for a junior

Knows the syntax (static class Nested inside another class) and that you instantiate it with new Outer.Nested() directly.

for a middle

Can articulate that 'static' means it belongs to the class not an instance, knows it can reach the outer class's private static members, and names a real example like Map.Entry or a Builder.

for a senior

Distinguishes it cleanly from an inner class, explains the access semantics, and justifies choosing a static nested class for scoping/cohesion over a top-level class.

for a principal

Frames nested-class choice as an API-design decision (encapsulation boundaries, what to expose vs. hide, memory implications of avoiding the implicit outer reference) and can reason about it at scale.

## What "nested" means Java lets you define a class inside another class. Such a class is called a **nested class**. There are two broad families: - **Static nested classes** — declared with the `static` keyword. - **Inner classes** (non-static nested classes) — declared **without** `static`. This question is about the *static* kind. ## Declaration You declare a static nested class by writing a class as a `static` member of an **enclosing class** (often called the *outer* class): ```java class Outer { static class Nested { void hello() { System.out.println("hi"); } } } ``` The word **static** here means the same thing it means for a static field or static method: the member belongs to the *class*, not to any *instance* (object) of that class. ## How you use it Because it belongs to the class rather than an instance, you create it without needing an `Outer` object: ```java Outer.Nested n = new Outer.Nested(); n.hello(); ``` Contrast this with a non-static inner class, which would require an outer instance first: `Outer.Inner i = new Outer().new Inner();`. ## Key terms defined - **Enclosing / outer class**: the class that physically contains the nested class declaration. - **Instance**: a concrete object created with `new`. "Static" things exist without any instance. - **Member**: anything declared directly inside a class body — fields, methods, and nested classes are all members. ## Why use one A static nested class is essentially a normal top-level class that has been *namespaced* inside another class to express that they belong together. Use it when a helper type is meaningful only in the context of the outer class — for example `Map.Entry` lives inside `Map`, or a `Builder` lives inside the class it builds. It keeps the type scoped and discoverable without leaking a separate top-level name into the package. ## Access rules A static nested class can access the **static** members (including `private` static fields/methods) of its enclosing class. It cannot access the enclosing class's *instance* fields directly, because it has no enclosing instance — that is the defining difference from an inner class, covered in the related question.

  • Can a static nested class be private?
    Yes. Like any class member it can be private, package-private, protected, or public. A private static nested class is visible only inside the enclosing class, which is common for internal helpers (e.g. a linked-list `Node`).
  • Does the JLS even call it an 'inner class'?
    No. The Java Language Specification reserves 'inner class' for non-static nested classes. A static nested class is explicitly NOT an inner class, precisely because it has no enclosing instance.

Think of the outer class as a building and a static nested class as a storage room labeled with that building's name on the directory. You can walk into the room straight from the lobby — you don't need to be a tenant (an instance) first. An inner class is more like a room that only exists once a specific tenant has moved in.

saying these in an interview costs you the question

  • Calling a static nested class an 'inner class' — the JLS reserves that term for non-static ones.
  • Saying you need an Outer instance to create it (you do not — that's the inner-class rule).
  • Claiming a static nested class can access the outer class's instance (non-static) fields directly.

context