What is a Nested Class in Java?
A nested class is a class declared inside another class. Java allows you to logically group classes that are only used in one place, which helps in improving code readability and encapsulation.
Types of Nested Classes in Java
- Non-static (Inner) Class
- Static Nested Class
- Local Inner Class (inside a method)
- Anonymous Inner Class (inline class for one-time use)
Non-static Inner Class
- Has access to all members (even private) of the outer class.
- Needs an object of the outer class to be instantiated.
class Outer {
int outerData = 10;
class Inner {
void display() {
System.out.println("Accessing outer data: " + outerData);
}
}
}
public class InnerClassExample {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // Create inner class object
inner.display(); // Output: Accessing outer data: 10
}
}
Static Nested Class
- Can only access static members of the outer class.
- Does not require outer class object to be instantiated.
class Outer {
static int data = 50;
static class Nested {
void show() {
System.out.println("Static data: " + data);
}
}
}
public class StaticNestedExample {
public static void main(String[] args) {
Outer.Nested obj = new Outer.Nested(); // No outer object needed
obj.show(); // Output: Static data: 50
}
}
Local Inner Class (inside a method)
- Declared inside a method.
- Can access final or effectively final variables of the method.
class Outer {
void outerMethod() {
int number = 100; // effectively final
class LocalInner {
void print() {
System.out.println("Local inner class: " + number);
}
}
LocalInner obj = new LocalInner();
obj.print();
}
}
public class LocalInnerExample {
public static void main(String[] args) {
Outer outer = new Outer();
outer.outerMethod(); // Output: Local inner class: 100
}
}
Anonymous Inner Class
- Used to override methods of a class or interface on the fly.
- Has no name.
- Mostly used with event handling or in multithreading.
abstract class Animal {
abstract void sound();
}
public class AnonymousInnerExample {
public static void main(String[] args) {
Animal obj = new Animal() {
void sound() {
System.out.println("Anonymous Inner Class: Dog barks");
}
};
obj.sound(); // Output: Anonymous Inner Class: Dog barks
}
}
Summary Table
Type | Can Access | Requires Outer Class Object | Use Case |
---|---|---|---|
Inner Class | All members | ✅ Yes | Grouping logic with outer class |
Static Nested Class | Only static members | ❌ No | Utility/helper class |
Local Inner Class | Local (final) vars | ✅ Yes (within method) | Temporary class inside method |
Anonymous Class | Depends | ✅ Yes | Quick one-time implementation |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.