When to use which type of nested class
- First of all, we need to understand when we needed to use a nested class?
- We use a nested class when a class is closely related to and only needed by its parent class.
-
// example class Car { void start() { Engine engine = new Engine(); engine.run(); } class Engine { void run() { System.out.println("Engine is running"); } } }
Java offers four types of nested classes to help you keep related logic together. But when should you use each of them?
Let’s simplify this with real-world cases and examples.
1. Static Nested Class
When to Use:
-
When the nested class does not need access to the outer class's instance variables.
-
To group utility/helper classes that are only relevant to the enclosing class.
-
To improve encapsulation and organization.
Example:
class Utils {
static class Logger {
static void log(String msg) {
System.out.println("LOG: " + msg);
}
}
}
2. Inner Class (member inner class ) (Non-static Nested Class)
When to Use:
-
When the nested class needs access to outer class’s instance data.
- if it does not need any instance data then choose the static ones
-
When the inner class logically belongs to one instance of the outer class.
-
Ideal for situations where behavior is tightly coupled with a specific object.
Example:
class Engine {
private int rpm = 6000;
class Piston {
void move() {
System.out.println("Piston moving at " + rpm + " rpm");
}
}
}
Remember:
You must create an object of the outer class to use the inner class.
3. Local Inner Class
When to Use:
-
When the class is used only within a single method.
-
To encapsulate helper logic that’s too long for a lambda, but still method-scoped.
-
Good for event handling, temporary state, or encapsulated processing.
Example:
void calculate() {
class Multiplier {
int timesTwo(int x) {
return x * 2;
}
}
Multiplier m = new Multiplier();
System.out.println(m.timesTwo(5));
}
Think of it as:
A private, one-method-only class that’s hidden from the rest of your app.
4. Anonymous Inner Class
When to Use:
-
When you need a one-time implementation of an interface or abstract class.
-
Great for short-lived event handlers, runnables, or comparators.
-
Saves time and code when no reuse is needed.
Example:
interface Hey {
void run();
}
public class Main {
public static void main(String[] args) {
Hey task = new Hey() {
@Override
public void run() {
System.out.println("Task is running (anonymous class)");
}
};
task.run(); // works
Hey task2 = new Hey(); // Error: Cannot instantiate the type Hey
}
}
Perfect for:
Replacing full class definitions when the logic is quick and single-use.
Summary
- Static nested class = when we need a class inside class and don't require any instance variables.
- Inner class = when we need a class inside class and require instance variables.
- Local Inner class = when we need a class inside a method of parent class for better encapsulation and reusability.
- Anonymous Inner class = when we need a one-time implementation of an interface or abstract class inside a class.