Nested and Inner Classes in Java
0 245
📦 Nested and Inner Classes in Java
In Java, it’s not just methods and variables that can exist within a class — even another class can be defined inside a class. This is where the concept of Nested and Inner Classes in Java comes into play.
It allows you to logically group classes, improve encapsulation, and reduce clutter.
In this tutorial, we’ll explore what these classes are, how they work, and why you might want to use them.
🧩 What are Nested and Inner Classes?
A nested class is a class defined inside another class. If it is declared static
, it's called a static nested class. If it is not static, it’s an inner class.
This nesting can help keep your code clean and tightly scoped, especially when one class is only useful to another.
🔍 Types of Nested and Inner Classes
- Static Nested Class
- Member Inner Class
- Local Inner Class (inside a method)
- Anonymous Inner Class (unnamed and used for instantiation on the fly)
🧱 Static Nested Class
A static nested class behaves like a static member of the outer class. It can access only the static members of the outer class.
class Outer {
static int outerValue = 50;
static class StaticNested {
void show() {
System.out.println("Static Nested Value: " + outerValue);
}
}
}
To access it:
Outer.StaticNested obj = new Outer.StaticNested();
obj.show();
👥 Member Inner Class
This is a non-static class inside another class. It can access both static and non-static members of the outer class.
class Outer {
int data = 100;
class Inner {
void display() {
System.out.println("Data is: " + data);
}
}
}
To use it:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
🔄 Local Inner Class
Defined within a method of the outer class, it can only be used within that method.
class Outer {
void outerMethod() {
int number = 5;
class LocalInner {
void print() {
System.out.println("Local number: " + number);
}
}
LocalInner local = new LocalInner();
local.print();
}
}
⚡ Anonymous Inner Class
Used for quick implementation of interfaces or classes, especially useful for event handling or short-lived tasks.
abstract class Greeting {
abstract void sayHello();
}
public class Test {
public static void main(String[] args) {
Greeting greet = new Greeting() {
void sayHello() {
System.out.println("Hello from Anonymous Inner Class!");
}
};
greet.sayHello();
}
}
🎯 Why Use Nested and Inner Classes?
- Logical Grouping: Keeps code that’s only relevant to one class, inside it.
- Improved Encapsulation: Reduces scope leakage of classes.
- Readable Code: Especially helpful for UI handling, event-driven code, etc.
🚀 Conclusion
Nested and Inner Classes in Java offer powerful ways to structure code cleanly and meaningfully. From static helpers to local implementations and anonymous quick-fixes, these tools help Java developers write more maintainable, readable, and encapsulated applications.
Mastering them adds a sharp edge to your Java skills!
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments