Nested Classes
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:
- Non-static (Inner) Class
- Static Nested Class
- Local Inner Class (defined inside a method)
- Anonymous Inner Class
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.
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.

About the authorMallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗