Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Nested Classes
Inner, Static, Anonymous, and Local



In Java, it's possible to define a class within another class. These are called nested classes. Think of it as a way to logically group classes that are only used in one place, helping keep your code organized and readable.

Nesting also allows you to access the members of the outer class, which can lead to more concise and tightly coupled code which is useful when designing object-oriented solutions that mirror real-world relationships.

Types of Nested Classes in Java

Java provides four types of nested classes:

1. Inner Class (Non-static)

An inner class is a non-static nested class. It has access to all the members (including private) of the outer class.

class Outer {
    private String msg = "Hello from Outer";

    class Inner {
        void showMessage() {
            System.out.println(msg);
        }
    }

    void demo() {
        Inner inner = new Inner();
        inner.showMessage();
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.demo();
    }
}
Hello from Outer

Explanation

Here, the Inner class can directly access the private field msg of the Outer class. This is a strong example of encapsulation at work.

2. Static Nested Class

This class is defined with the static keyword. Unlike inner classes, it cannot access non-static members of the outer class directly.

class Container {
    static int data = 30;

    static class StaticInner {
        void display() {
            System.out.println("Data is: " + data);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Container.StaticInner obj = new Container.StaticInner();
        obj.display();
    }
}
Data is: 30

Explanation

Because StaticInner is static, it can be instantiated without creating an instance of the outer class. But it can only access static members of the outer class.

3. Local Inner Class

These are classes defined within a method. They are not visible outside the method and are useful for encapsulating helper functionality.

class LocalDemo {
    void process() {
        class Helper {
            void help() {
                System.out.println("Helping from inside the method");
            }
        }
        Helper helper = new Helper();
        helper.help();
    }
}

public class Main {
    public static void main(String[] args) {
        new LocalDemo().process();
    }
}
Helping from inside the method

Explanation

The class Helper lives inside the method process. It's short-lived and only exists for that method's lifecycle — ideal when you want temporary behavior tightly coupled with the method logic.

4. Anonymous Inner Class

An anonymous inner class is a class without a name, used to instantiate classes or interfaces on the fly — most commonly used for implementing interfaces or abstract classes.

abstract class Greeting {
    abstract void sayHello();
}

public class Main {
    public static void main(String[] args) {
        Greeting g = new Greeting() {
            void sayHello() {
                System.out.println("Hello from anonymous class!");
            }
        };
        g.sayHello();
    }
}
Hello from anonymous class!

Explanation

Here, instead of defining a separate subclass for Greeting, we define and instantiate it in one go. It’s perfect when you need a one-time implementation.

Use Cases of Nested Classes

Conclusion

Nested classes in Java organize code in a more meaningful way. From tightly coupled inner classes to ad-hoc anonymous implementations, they offer multiple flavors of encapsulation.

QUIZ

Question 1:Which statement correctly describes an inner (non-static) class in Java?

Question 2:What will be the output of the following code?
class Outer {
    private String msg = "Hello from Outer";

    class Inner {
        void showMessage() {
            System.out.println(msg);
        }
    }

    void demo() {
        Inner inner = new Inner();
        inner.showMessage();
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.demo();
    }
}

Question 3:A static nested class in Java can directly access non-static members of the outer class.

Question 4:Which of the following are true about local inner classes?

Question 5:What is the primary use of anonymous inner classes in Java?

Question 6:Which of the following are valid types of nested classes in Java?



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