Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Nested Static Class
Examples and Explanation



In Java, classes can be defined inside other classes — these are called nested classes. When a nested class is marked as static, it becomes a nested static class. This concept might seem a little strange at first, but it's extremely useful when a class logically belongs to another class, yet does not require access to the outer class's instance variables or methods.

Why Use a Static Nested Class?

Syntax of a Static Nested Class

class OuterClass {
    static class NestedStaticClass {
        void display() {
            System.out.println("Inside static nested class.");
        }
    }
}

How to Create an Object of a Static Nested Class

Because the nested class is static, you don’t need an instance of the outer class to create its object. Here's how you do it:

public class Test {
    public static void main(String[] args) {
        OuterClass.NestedStaticClass obj = new OuterClass.NestedStaticClass();
        obj.display();
    }
}
Inside static nested class.

Explanation

We define NestedStaticClass inside OuterClass and mark it as static. In main(), we directly access the static nested class using the syntax OuterClass.NestedStaticClass without needing an object of OuterClass.

Accessing Static Members of Outer Class

A static nested class can access all static members (variables and methods) of its outer class:

class Outer {
    static int outerValue = 42;

    static class StaticInner {
        void show() {
            System.out.println("Outer static value: " + outerValue);
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        Outer.StaticInner obj = new Outer.StaticInner();
        obj.show();
    }
}
Outer static value: 42

Limitations of Static Nested Classes

Static nested classes cannot access non-static members of the outer class directly. If you try to do so, you’ll get a compilation error. Here’s an example:

class Outer {
    int outerInstanceVar = 5;

    static class StaticInner {
        void show() {
            // System.out.println(outerInstanceVar); // ❌ Error: Cannot access non-static variable
        }
    }
}

Nested Static Class with Constructor

Just like any other class, you can add constructors to static nested classes:

class Book {
    static class Publisher {
        String name;

        Publisher(String name) {
            this.name = name;
        }

        void print() {
            System.out.println("Published by: " + name);
        }
    }
}
public class DemoPublisher {
    public static void main(String[] args) {
        Book.Publisher p = new Book.Publisher("TutorialKart Publishing");
        p.print();
    }
}
Published by: TutorialKart Publishing

Real-World Analogy

Imagine you’re designing a Car class. You might define a static nested class called EngineSpecs inside Car because it logically belongs to Car but doesn't need direct access to car's instance-level properties (like color or mileage).

When Should You Use Static Nested Classes?

Quick Recap

Feature Static Nested Class
Access outer class static members ✅ Yes
Access outer class non-static members ❌ No
Instance needed to use ❌ No outer class instance required
Use case Helper class tightly coupled to outer class

Conclusion

The static nested class in Java is a neat tool to structure your code better, especially when dealing with logically grouped elements. While it can’t access instance variables of the outer class, its ability to operate independently makes it a clean and encapsulated design choice.

QUIZ

Question 1:Which of the following best describes a Java static nested class?

Question 2:Static nested classes in Java can directly access both static and non-static members of the outer class.

Question 3:Which of the following are valid reasons to use a static nested class in Java?

Question 4:What will be the output of the following Java program?
class Outer {
    static class Inner {
        void show() {
            System.out.println("Hello from static nested class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Inner obj = new Outer.Inner();
        obj.show();
    }
}

Question 5:Static nested classes can have their own constructors, fields, and methods just like regular classes.

Question 6:In the context of static nested classes, which statements are true?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M