Home Java Java Static Nested Class

Java Static Nested Class

Beginner ⏱ 8 min read Updated: Jul 2026

A static nested class in Java is a class declared inside another class using the static keyword. Unlike a normal inner class, a static nested class does not require an object of the outer class to be created. It can directly access only the static members of the outer class.

Static nested classes help organize related classes, improve code readability, and reduce memory usage because they do not keep a reference to an instance of the outer class.

What is a Static Nested Class?

A static nested class is simply a nested class that belongs to the outer class rather than to an object of the outer class. It behaves similarly to other static members, such as static variables and static methods.

💡
Key Point: Java does not allow top-level static classes. Only nested classes can be declared as static.

Syntax of Static Nested Class

Syntax
class OuterClass {
    static class NestedClass  {
    }
 }

Example of Static Nested Class

Example
class Outer {
    static int number = 100;
    static class Inner {
        void display() {
            System.out.println(number);
        }
    }
    public static void main(String[] args) {
        Outer.Inner obj = new Outer.Inner();
        obj.display();
    }
}
Output
100

How Static Nested Classes Work

Since the nested class is declared as static, it becomes a member of the outer class rather than an instance of it. Therefore, you can create its object without creating an object of the outer class.

A static nested class can directly access static variables and static methods of the outer class. To access non-static members, it needs an object of the outer class.

Advantages of Static Nested Classes

Better Organization

Keeps related classes together inside one outer class.

Memory Efficient

Does not maintain a reference to the outer class object.

Easy Access

Can directly access static members of the outer class.

Reusable

Useful for helper classes that belong only to one outer class.

Static Nested Class vs Inner Class

  • Static nested classes use the static keyword.
  • Inner classes do not use the static keyword.
  • Static nested classes do not need an outer class object.
  • Inner classes require an object of the outer class.
  • Static nested classes directly access only static members.
  • Inner classes can access both static and non-static members.

When to Use Static Nested Classes

Static nested classes are useful when a helper class is closely related to its outer class but does not need access to instance members. They are commonly used in utility classes, builders, configuration classes, and data structures.

Important Points

  • Only nested classes can be declared static.
  • Top-level classes cannot be static.
  • Static nested classes belong to the outer class.
  • They can access static members directly.
  • They improve code organization and reduce memory overhead.
  • Create objects using OuterClass.InnerClass.

Summary

A static nested class is a nested class declared with the static keyword. It belongs to the outer class instead of its objects, making it lightweight and easy to use. Static nested classes are ideal for grouping helper classes that do not need access to instance members. Understanding static nested classes helps developers write cleaner, more organized, and maintainable Java applications.