In Java, classes are usually defined separately.
But sometimes, it makes logical sense to define a class inside another class ๐งฉ
This concept is called Nested Classes.
Nested classes improve:
- Code organization ๐ฆ
- Logical grouping ๐ง
- Encapsulation ๐
- Readability โจ
Java provides powerful mechanisms to create classes within classes, and understanding them deeply helps in writing structured and professional code.
This article explains Nested Classes and Inner Classes in Java in complete detail โ with clear examples, step-by-step explanations, and common pitfalls.
๐ง What Is a Nested Class?
A Nested Class is a class defined inside another class.
There are two main types:
1๏ธโฃ Static Nested Class
2๏ธโฃ Inner Class (Non-static Nested Class)
Additionally, Java also supports:
3๏ธโฃ Local Inner Class
4๏ธโฃ Anonymous Inner Class
๐๏ธ 1๏ธโฃ Static Nested Class
A static nested class is declared using the static keyword.
Key Characteristics:
- Belongs to outer class
- Does NOT require outer object
- Can access only static members of outer class directly
โถ๏ธ Example 1: Static Nested Class
class Outer {
static int x = 10;
static class Inner {
void display() {
System.out.println("Value of x: " + x);
}
}
}
public class StaticNestedDemo {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}
๐ง Step-by-Step Explanation
Outeris outer classInneris static nested classxis static variableInneraccessesxdirectly- No
Outerobject required
๐จ Output:
Value of x: 10
โ ๏ธ Important Rule
Static nested class:
- Cannot access non-static members directly โ
๐๏ธ 2๏ธโฃ Non-Static Inner Class
A non-static nested class is simply called an Inner Class.
Key Characteristics:
- Requires outer class object
- Can access all members (including private)
- Associated with specific outer object
โถ๏ธ Example 2: Inner Class
class Outer {
int x = 20;
class Inner {
void display() {
System.out.println("Value of x: " + x);
}
}
}
public class InnerClassDemo {
public static void main(String[] args) {
Outer outerObj = new Outer();
Outer.Inner innerObj = outerObj.new Inner();
innerObj.display();
}
}
๐ง Step-by-Step Explanation
1๏ธโฃ Outer object is created
2๏ธโฃ Inner object created using outerObj.new Inner()
3๏ธโฃ Inner class accesses outer variable x
4๏ธโฃ Output displayed
๐จ Output:
Value of x: 20
๐ Why Does Inner Class Need Outer Object?
Because:
- Inner class is associated with specific outer instance
- It can access non-static members
๐ง Memory Concept
For Inner Class:
- Each inner object is tied to outer object
- Cannot exist independently
For Static Nested Class:
- Acts like normal class
- No outer reference stored
๐๏ธ 3๏ธโฃ Local Inner Class
A class defined inside a method.
โถ๏ธ Example 3: Local Inner Class
class Outer {
void show() {
class LocalInner {
void display() {
System.out.println("Inside Local Inner Class");
}
}
LocalInner obj = new LocalInner();
obj.display();
}
}
public class LocalInnerDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.show();
}
}
๐ง Explanation
LocalInnerexists only insideshow()- Cannot be accessed outside method
- Scope limited to method block
๐๏ธ 4๏ธโฃ Anonymous Inner Class
A class without a name.
Used mainly for:
- Implementing interfaces
- Overriding methods
โถ๏ธ Example 4: Anonymous Inner Class
abstract class Greeting {
abstract void sayHello();
}
public class AnonymousInnerDemo {
public static void main(String[] args) {
Greeting g = new Greeting() {
void sayHello() {
System.out.println("Hello from Anonymous Class");
}
};
g.sayHello();
}
}
๐ง Explanation
- Anonymous class extends
Greeting - Provides implementation
- No separate class definition required
- Used once
๐ Summary Table
| Type | Requires Outer Object | Can Access Private Members | Typical Use |
|---|---|---|---|
| Static Nested | โ | Only static | Utility grouping |
| Inner Class | โ | Yes | Strong coupling |
| Local Inner | โ | Yes | Method-level logic |
| Anonymous | Depends | Yes | Quick implementation |
โ Common Confusions
โ Is inner class same as static nested class?
โ No
Static nested class behaves like normal class.
โ Can inner class have static members?
โ Only if they are constants.
โ Can outer class access inner members?
Yes, if object exists.
โ Common Mistakes
โ Forgetting outer object for inner class
Outer.Inner obj = new Outer.Inner(); // โ ERROR
Correct:
Outer outerObj = new Outer();
Outer.Inner obj = outerObj.new Inner();
โ Trying to access non-static member in static nested class
Compilation error โ
โ Overusing inner classes unnecessarily
Can make code confusing.
๐งช Tricky Example
class Outer {
int x = 5;
class Inner {
int x = 10;
void display() {
System.out.println(x);
System.out.println(Outer.this.x);
}
}
}
public class TrickyInnerDemo {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
i.display();
}
}
๐จ Output:
10
5
๐ง Explanation
xrefers to inner variableOuter.this.xrefers to outer variable
๐ฏ Best Practices
- Use nested classes for logical grouping
- Use static nested class when no outer instance needed
- Avoid unnecessary complexity
- Keep design simple and readable
โ Conclusion
Nested and inner classes provide powerful tools for organizing and structuring Java code ๐
By understanding:
- Static nested classes
- Non-static inner classes
- Local inner classes
- Anonymous inner classes
- Object relationships and memory behavior
You gain deeper control over class design and encapsulation ๐ช
When used appropriately, nested classes enhance clarity and maintainability.
When misused, they increase confusion.
Mastery of this topic strengthens understanding of advanced features such as:
- Event handling
- GUI programming
- Functional programming concepts