Java Inner Classes in Java
Inner Classes in Java
Inner classes are classes that are defined inside another class. They are a powerful feature in Java, providing a way to logically group classes that are only used in one place. Inner classes can access the members (fields and methods) of the outer class, and they can be used to implement certain behaviors in a cleaner and more readable manner.
There are four types of inner classes in Java:
Non-static Inner Class (Member Inner Class)
Static Inner Class (Static Nested Class)
Local Inner Class
Anonymous Inner Class
1. Non-static Inner Class (Member Inner Class)
A non-static inner class is associated with an instance of the outer class. It can access all the members (both static and non-static) of the outer class.
Syntax:
class OuterClass { private int outerVar = 10; class InnerClass { void display() { System.out.println("Outer variable: " + outerVar); } }}Example:
class OuterClass { private int outerVar = 10; // Non-static inner class class InnerClass { void display() { System.out.println("Outer variable: " + outerVar); } }}public class InnerClassExample { public static void main(String[] args) { // Creating an instance of the outer class OuterClass outer = new OuterClass(); // Creating an instance of the inner class OuterClass.InnerClass inner = outer.new InnerClass(); inner.display(); // Calls the inner class method }}Output:
Outer variable: 10Explanation:
In this example, the
InnerClassis a non-static inner class that can access theouterVarvariable of theOuterClassdirectly.To instantiate the inner class, we need an instance of the outer class (
outer.new InnerClass()).
2. Static Inner Class (Static Nested Class)
A static inner class is not associated with an instance of the outer class. It can only access the static members of the outer class. You can instantiate a static inner class without creating an instance of the outer class.
Syntax:
class OuterClass { private static int outerVar = 10; static class InnerClass { void display() { System.out.println("Outer variable: " + outerVar); } }}Example:
class OuterClass { private static int outerVar = 10; // Static inner class static class InnerClass { void display() { System.out.println("Outer variable: " + outerVar); } }}public class StaticInnerClassExample { public static void main(String[] args) { // Creating an instance of the static inner class OuterClass.InnerClass inner = new OuterClass.InnerClass(); inner.display(); // Calls the static inner class method }}Output:
Outer variable: 10Explanation:
In this case,
InnerClassis a static inner class and can access only static members ofOuterClass.We instantiate the static inner class using
OuterClass.InnerClasswithout needing an instance ofOuterClass.
3. Local Inner Class
A local inner class is defined within a method, constructor, or block. It can only be used within the method, constructor, or block where it is defined.
Syntax:
class OuterClass { void someMethod() { class InnerClass { void display() { System.out.println("Inside method"); } } InnerClass inner = new InnerClass(); inner.display(); }}Example:
class OuterClass { void someMethod() { // Local inner class defined within the method class InnerClass { void display() { System.out.println("Inside method"); } } // Creating an instance of the local inner class InnerClass inner = new InnerClass(); inner.display(); // Calls the inner class method }}public class LocalInnerClassExample { public static void main(String[] args) { OuterClass outer = new OuterClass(); outer.someMethod(); // Calls the method containing the local inner class }}Output:
Inside methodExplanation:
The
InnerClassis defined inside the methodsomeMethodof theOuterClass. It is local to this method and cannot be accessed outside of it.We instantiate
InnerClassinsidesomeMethod()and call itsdisplay()method.
4. Anonymous Inner Class
An anonymous inner class is a class that is defined without a name. It is often used when you need a class to implement an interface or extend a class once. It is typically used when you want to override methods on the fly.
Syntax:
interface MyInterface { void display();}public class AnonymousInnerClassExample { public static void main(String[] args) { MyInterface obj = new MyInterface() { @Override public void display() { System.out.println("Inside anonymous inner class"); } }; obj.display(); // Calls the overridden method in the anonymous class }}Output:
Inside anonymous inner classExplanation:
Here, the anonymous inner class is created by implementing the
MyInterfaceinterface.Since there is no need to explicitly define a class, the class is created and used instantly at the point of instantiation.
The method
display()is overridden inside the anonymous class.
Advantages of Inner Classes:
Encapsulation: Inner classes help encapsulate a class that is only used within its outer class.
Code Organization: Helps organize code that is logically related but not used elsewhere.
Access to Outer Class Members: Inner classes have access to both static and non-static members of the outer class.
Event Handling: Inner classes are widely used in event handling in Java (e.g., GUI components).
Use Cases of Inner Classes:
Implementing Event Listeners: Inner classes are often used in GUI frameworks (like Swing and JavaFX) to implement event listeners.
Data Structures: Used in data structures where a class needs to represent an element that only makes sense in the context of the parent class.
Callback Functions: Inner classes can be used to implement callback functions, especially when using anonymous inner classes.
Conclusion
Inner classes are a powerful feature in Java that help in organizing and grouping related functionality. They are particularly useful in situations where you need to access the outer class's instance variables and methods directly, or when you want to create a small class to handle a specific task that does not need to be used elsewhere.
Let me know if you'd like more examples or further clarification! ?