Nested Classes
Nested Classes
A school program needs reliable data and clear rules. A nested class organizes a type inside another type. A non-static inner class has an enclosing instance; a static nested class does not require one.
Understand
A nested class organizes a type inside another type.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Static and non-static nested classes have different construction syntax and access to enclosing instance state.
Nested Classes
In Java, it's possible to define a class within another class. These are called nested classes. Think of it as a way to logically group classes that are only used in one place, helping keep your code organized and readable.
Nesting also allows you to access the members of the outer class, which can lead to more concise and tightly coupled code which is useful when designing object-oriented solutions that mirror real-world relationships.
Types of Nested Classes in Java
Java provides four types of nested classes:
- Non-static (Inner) Class
- Static Nested Class
- Local Inner Class (defined inside a method)
- Anonymous Inner Class
1. Inner Class (Non-static)
An inner class is a non-static nested class. It has access to all the members (including private) of the outer class.
class Outer {
private String msg = "Hello from Outer";
class Inner {
void showMessage() {
System.out.println(msg);
}
}
void demo() {
Inner inner = new Inner();
inner.showMessage();
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.demo();
}
}
Hello from Outer
Explanation
Here, the Inner class can directly access the private field msg of the Outer class. This is a strong example of encapsulation at work.
2. Static Nested Class
This class is defined with the static keyword. Unlike inner classes, it cannot access non-static members of the outer class directly.
class Container {
static int data = 30;
static class StaticInner {
void display() {
System.out.println("Data is: " + data);
}
}
}
public class Main {
public static void main(String[] args) {
Container.StaticInner obj = new Container.StaticInner();
obj.display();
}
}
Data is: 30
Explanation
Because StaticInner is static, it can be instantiated without creating an instance of the outer class. But it can only access static members of the outer class.
3. Local Inner Class
These are classes defined within a method. They are not visible outside the method and are useful for encapsulating helper functionality.
class LocalDemo {
void process() {
class Helper {
void help() {
System.out.println("Helping from inside the method");
}
}
Helper helper = new Helper();
helper.help();
}
}
public class Main {
public static void main(String[] args) {
new LocalDemo().process();
}
}
Helping from inside the method
Explanation
The class Helper lives inside the method process. It's short-lived and only exists for that method's lifecycle — ideal when you want temporary behavior tightly coupled with the method logic.
4. Anonymous Inner Class
An anonymous inner class is a class without a name, used to instantiate classes or interfaces on the fly — most commonly used for implementing interfaces or abstract classes.
abstract class Greeting {
abstract void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = new Greeting() {
void sayHello() {
System.out.println("Hello from anonymous class!");
}
};
g.sayHello();
}
}
Hello from anonymous class!
Explanation
Here, instead of defining a separate subclass for Greeting, we define and instantiate it in one go. It’s perfect when you need a one-time implementation.
Use Cases of Nested Classes
- Logically grouping classes that are only used in one place
- Increased encapsulation and abstraction
- Cleaner code with minimal external access
- Short-lived functionality using local or anonymous classes
Conclusion
Nested classes in Java organize code in a more meaningful way. From tightly coupled inner classes to ad-hoc anonymous implementations, they offer multiple flavors of encapsulation.
Important boundaries and mistakes
Static and non-static nested classes have different construction syntax and access to enclosing instance state.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Static and non-static nested classes have different construction syntax and access to enclosing instance state..
Java 17 · Main.java · complete practice program
public class Main {
static class SchoolRoom {
static class Label {
String text = "Class 10A";
}
}
public static void main(String[] args) {
System.out.println(new SchoolRoom.Label().text);
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
static class SchoolRoom {
static class Label {
String text = "Class 10A";
}
}
public static void main(String[] args) {
System.out.println(new SchoolRoom.Label().text);
}
}
Expected output
Class 10AA nested class organizes a type inside another type. A non-static inner class has an enclosing instance; a static nested class does not require one.
Static and non-static nested classes have different construction syntax and access to enclosing instance state.
Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.
Check your understanding
What to remember
A nested class organizes a type inside another type. A non-static inner class has an enclosing instance; a static nested class does not require one. Static and non-static nested classes have different construction syntax and access to enclosing instance state.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.