In Java, nested classes and inner classes provide a way to group classes within another class, allowing for better organization of code and encapsulation of functionality. There are two types of nested classes: static nested classes and inner classes.
Static Nested Classes
A static nested class is a class declared as a static member of another class. It is associated with the enclosing class but does not have access to instance-specific members of the outer class.
Here’s an example that demonstrates the usage of Static Nested classes:
class OuterClass { private static String outerMessage = "Hello from OuterClass"; // Static nested class public static class StaticNestedClass { public void display() { System.out.println(outerMessage); // Accessing static member of outer class } } } class StaticNestedDemo{ public static void main(String[] args) { // Creating an instance of the static nested class OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass(); // Calling the display method of the nested class nestedObj.display(); } }
Output:
D:\>java StaticNestedDemo
Hello from OuterClass
Code language: JavaScript (javascript)
In this example, StaticNestedClass is a static nested class within OuterClass. It can access the static members of OuterClass directly.
Inner Classes
An inner class is a non-static nested class that is associated with an instance of the enclosing class. It can access both static and instance-specific members of the outer class. There are four types of inner classes: member inner classes, local inner classes, and anonymous inner classes.
Member Inner Class
Here’s an example that demonstrates the usage of Member Inner classes:
class OuterClass { private String outerMessage = "Hello from OuterClass"; // Member inner class public class InnerClass { public void display() { System.out.println(outerMessage); // Accessing instance member of outer class } } } class MemberInnerDemo{ public static void main(String[] args) { // Creating an instance of the outer class OuterClass outerObj = new OuterClass(); // Creating an instance of the inner class using the outer class instance OuterClass.InnerClass innerObj = outerObj.new InnerClass(); // Calling the display method of the inner class innerObj.display(); } }
Output:
D:\>java MemberInnerDemo
Hello from OuterClass
Code language: JavaScript (javascript)
In this example, InnerClass is a member inner class within OuterClass. It can access both static and instance members of OuterClass.
  Local Inner Class
Here’s an example that demonstrates the usage of Local Inner classes:
class OuterClass { private String outerMessage = "Hello from OuterClass"; // Method with a local inner class public void outerMethod() { class LocalInnerClass { public void display() { System.out.println(outerMessage); // Accessing instance member of outer class } } // Creating an instance of the local inner class LocalInnerClass localObj = new LocalInnerClass(); // Calling the display method of the local inner class localObj.display(); } } class LocalInnerDemo{ public static void main(String[] args) { // Creating an instance of the outer class OuterClass outerObj = new OuterClass(); // Calling the outerMethod to create and use the local inner class outerObj.outerMethod(); } }
Output:
D:\>java LocalInnerDemo
Hello from OuterClass
Code language: JavaScript (javascript)
Anonymous Inner classes
An anonymous inner class is a local class without a name. It’s often used for creating an instance of a class and overriding its methods on-the-fly.
Here’s an example that demonstrates the usage of Anonymous Inner classes:
public class India { public static void main(String[] args) { // Creating an object of an anonymous inner class representing Andhra Pradesh State andhraPradesh = new State() { // Anonymous inner class body @Override void stateInfo() { System.out.println("Andhra Pradesh is a state in the southeastern part of India."); } }; // Calling the method of the anonymous inner class andhraPradesh.stateInfo(); } // Abstract class representing a state static abstract class State { abstract void stateInfo(); } }
Output:
D:\>javac India.java
D:\>java India
Andhra Pradesh is a state in the southeastern part of India.
Code language: CSS (css)