Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
Nested Classes in Java with Examples
Nested classes in Java are inner classes that are defined within the body of another class. They can be used to logically group related classes, improve code readability, and increase encapsulation. In this tutorial, we'll explore the different types of nested classes and how to use them in Java.
In Java, a class can be nested inside another class, similar to the way that a method can be nested inside a class. This is called a nested class, and it can be one of four types: simple inner class, method-local inner class, anonymous inner class, and static nested class. Each type of nested class has its own unique characteristics and use cases, which we will explore in this article.
1. Simple Inner Classes
An inner class is a class that is defined inside another class. It has access to all of the members (fields and methods) of the outer class, including private members. Inner classes are often used to provide additional functionality for the outer class, or to create more modular and reusable code.
Here is an example of an inner class in Java:
class OuterClass {
private int outerField = 0;
public void outerMethod() {
// Code goes here
}
class InnerClass {
// Inner class can access all members of the outer class
public void innerMethod() {
outerField = 1;
outerMethod();
}
}
}
To create an instance of the inner class, we must first create an instance of the outer class:
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
2. Method-Local Inner Classes
A method-local inner class is defined inside a method of the outer class. It can only be used within the method in which it is defined, and it does not have access to the members of the outer class.
Here is an example of a method-local inner class in Java:
class OuterClass {
public void outerMethod() {
int localVariable = 0;
class InnerClass {
// Inner class does not have access to outer class members
public void innerMethod() {
// Code goes here
}
}
InnerClass inner = new InnerClass();
inner.innerMethod();
}
}
3. Anonymous Inner Classes
An anonymous inner class is a class that is defined and instantiated in a single statement. It does not have a name, and it can only be used once. Anonymous inner classes are often used as a shortcut when creating a simple implementation of an interface or abstract class.
Here is an example of an anonymous inner class in Java:
class OuterClass {
public void outerMethod() {
// Anonymous inner class that implements the Runnable interface
Runnable r = new Runnable() {
public void run() {
// Code goes here
}
};
}
}
4. Static Nested Classes
A static nested class is a class that is defined inside another class and is marked with the static modifier. It has no access to the members of the outer class, and it is not associated with any instance of the outer class.
Here is an example of a static nested class in Java:
class OuterClass {
// Static Nested Class
static class StaticNestedClass {
public void nestedMethod() {
System.out.println("Inside Static Nested Class");
}
}
}
public class NestedClasses {
public static void main(String[] args) {
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.nestedMethod();
}
}