2. Introduction
Java allows classes to be defined within other classes.
These are called Nested Classes.
They help in logically grouping classes and controlling visibility.
Nested classes can access out class members and improve code modularity
3. Types of Nested Classes
1. Static Nested Class
2. Non-static Nested Class (Inner Class)
- Member Inner Class
- Local Inner Class
- Anonymous Inner Class
4. Static Nested Class - Basic Example
class Outer {
static int a = 10;
static class StaticNested {
void show() {
System.out.println("Static a: " + a);
}
}
}
Usage:
Outer.StaticNested obj = new Outer.StaticNested();
obj.show();
•Defined using static keyword inside another class
•Can access only static members of the outer class.
5. Static Nested Class -
Advanced Example
class MathUtil {
static class Calculator {
static int square(int x) {
return x * x;
}
}
}
Usage:
int result = MathUtil.Calculator.square(5);
System.out.println(result); // 25
6. Static Nested Class - Benefits
• Does not require access to outer class instance.
• Useful for grouping static helpers.
• Static nested classes allow you to organize related
static methods and constants within a containing class.
• This helps keep your code more structured and modular,
especially for tools, calculators, or common operations.
• Ideal for utility classes — classes that provide
commonly used static methods.
7. Inner Class - Basic Example
class Outer {
int a = 10;
class Inner {
void display() {
System.out.println("a: " + a);
}
}
}
Usage:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
•Non-static class defined inside another class.
•Can access both static and non-static members of the outer class.
•Needs an object of the outer class to be created.
8. Inner Class - Advanced Example
class Bank {
private String bankName = "ABC Bank";
class Account {
void display() {
System.out.println("Welcome to " + bankName);
}
}
}
Usage:
Bank bank = new Bank();
Bank.Account acc = bank.new Account();
acc.display();
Advantages:
• Inner class has full access to outer class members.
• Ideal when the inner class depends heavily on the outer class for its functionality.
9. Local Inner Class
class Outer {
void outerMethod() {
class LocalInner {
void msg() {
System.out.println("Inside Local Inner Class");
}
}
LocalInner obj = new LocalInner();
obj.msg();
}
}
Key Points:
•Defined inside a method.
•Can access final or effectively final variables from method.
10. Anonymous Inner Class
abstract class Animal {
abstract void sound();
}
class Test {
public static void main(String args[]) {
Animal dog = new Animal() {
void sound() {
System.out.println("Woof Woof");
}
};
dog.sound();
}
}
When to use:
• When creating an instance of class with certain methods overridden.
• We need to redefining or implementing a method immediately while creating the object
• Useful for one-time use or quick custom behaviour
11. GUI Example with Anonymous
Inner Class
JButton b = new JButton("Click Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
});
• Common in Swing/AWT for event handling.
• using an anonymous inner class (declared inline) avoids writing
a completely separate class just to handle an event like a
button click. It keeps the code shorter and more readable.
12. Comparison Table
Type Access to Outer Static? Use Case
Static
Nested
Static members
only
Yes Utility classes
Inner Class All members No
two classes are
strongly
dependent on
each other
Local Inner
Local method
scope
No Limited usage
Anonymous All members No One-time use
13. Best Practices
Use static nested class for utility/helper classes.
Use inner classes when tightly bound to outer class
logic.
Local inner classes are good for encapsulating
temporary logic
Anonymous classes are best suited for quick overrides.
14. Conclusion
• Nested classes help in logically structuring classes.
• Provide encapsulation and code grouping.
• Use the appropriate type based on your design
requirements.