- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java
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:
- 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.
Use Cases of Nested Classes
- Logically grouping classes that are only used in one place
- Increased encapsulation and abstraction
- Cleaner code with minimal external access
- Short-lived functionality using local or anonymous 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();
}
}
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();
}
}