HOWTO · Java

Inner Class and Static Nested Class in Java

In computer programming, a class is a blueprint that allows us to create individual class objects to access data from either data members or member functions. In Java programming, a class is defined with the keyword class. We can also say that a class is a user-defined data type with custom modifications.

On this page

A nested class is declared inside another class. Java supports static nested classes and non-static nested classes (inner classes). Use a static nested class when the type belongs conceptually with its enclosing class but does not need an enclosing object. The static keyword describes that relationship; it does not make the nested type a top-level class or provide a security boundary.

Static Nested Classes in Java

A static nested class is a member type declared inside another class with the static keyword. It can be named with the enclosing type, such as Envelope.Label, and can be instantiated without first creating an Envelope object. This is useful when a helper or value type is closely associated with one class and is not meaningful as a separate top-level type.

The nested type still has its own instances. Marking the nested class static does not make its fields or methods static. You can give the nested class its own constructors, instance state, and methods just as you would for a top-level class.

How Static Nested Classes Relate to Their Enclosing Class

A static nested class has no implicit reference to an enclosing-class instance. It can directly access the enclosing class’s static members, including private static members. It cannot directly use an enclosing instance field or method because there is no enclosing object associated with it. If it needs such data, pass an enclosing object to a constructor or method and access the member through that reference.

The reverse access relationship is also useful: enclosing code can work with a nested class, including its private members, subject to Java’s normal access rules. Private visibility controls which code can name a member; it should not be described as a security feature.

Declare and Instantiate a Static Nested Class

Declare the type in the enclosing class body using static class Label. From outside the enclosing class, refer to it as Envelope.Label. The following complete example keeps the nested value type grouped with the class that uses it and creates it directly from main:

Run this example:

public class Main {
    static class Label {
        private final String text;

        Label(String text) {
            this.text = text;
        }

        String display() {
            return "Label: " + text;
        }
    }

    public static void main(String[] args) {
        Label label = new Label("priority");
        System.out.println(label.display());
    }
}
Label: priority

Because main is a static method, it creates Label without constructing Main. Code in another class can use the qualified name Main.Label and call a suitably accessible constructor. When a nested class needs information from a particular enclosing object, make that dependency explicit by passing the object in.

Static Nested Classes vs. Inner Classes

An inner class is a non-static member class. Each inner-class object is associated with an instance of its enclosing class, and its code can refer to that enclosing instance’s members directly. To create one from outside the enclosing class, first create the outer object, then use the form outer.new Inner(); the inner type is named Outer.Inner.

A static nested class has no such association, so its construction does not use an enclosing object: use new Outer.Nested() from outside, or the short name where it is in scope. Both forms can be private or public according to the intended API, and both can access private members of the enclosing class. The distinction is the implicit enclosing-instance relationship, not a special security level.

When to Use a Static Nested Class

Choose a static nested class when the nested type is logically part of its enclosing class but each instance can do its work without an implicit enclosing object. Common fits include a small value type, a builder, or a helper whose meaning is specific to one enclosing API. A static nested class can still receive dependencies explicitly, which makes those requirements visible to callers.

Choose an inner class when each instance must work with the particular enclosing object that created it—for example, when it needs to read or update that object’s instance state. If the type has a broad independent role, a top-level class may be easier to reuse and discover. Decide from the relationship the code needs, then choose the narrowest visibility that supports its intended use.