Back to: Java Tutorials For Beginners and Professionals
Inner Classes in Java with Examples
In this article, I am going to discuss Inner Classes in Java with Examples. Please read our previous article, where we discussed Constructors in Java. At the end of this article, you will understand what are inner classes and their type and when and how to implement this in Java Applications.
What are Inner Classes in Java?
The inner class is defined inside the body of another class (known as an outer class). The class written within is called the nested class, and the class that holds the inner class is called the outer class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. The Syntax is given below.
Types of Inner Classes in Java:
The inner classes are of four types. They are :
- Nested Inner Class
- Method Local Inner Class
- Anonymous Inner Class
- Static Nested Class
Nested Inner Class in Java:
A class created within the class and outside the method is known as Nested Inner Class in Java. It can access the private instance variable of the outer class.
Example to Understand Nested Inner Classes in Java:
package Demo; public class NestedInnerClass { class Inner { public void show () { System.out.println ("In a nested class method"); } } public static void main (String[]args) { NestedInnerClass.Inner in = new NestedInnerClass ().new Inner (); in.show (); } }
Output: In a nested class method
Method Local Inner Class in Java:
A class created within the method of the enclosing class is known as Method Local Inner Class. Since the local inner class is not associated with Object, we can’t use private, public, or protected access modifiers with it. The only allowed modifiers are abstract or final.
Method Local Inner Class Example in Java:
package Demo; public class MethodLocalInnerClass { void outerMethod () { System.out.println ("Inside OuterMethod"); // Inner class is local to outerMethod() class Inner { void innerMethod () { System.out.println ("Inside InnerMethod"); } } Inner y = new Inner (); y.innerMethod (); } public static void main (String[]args) { MethodLocalInnerClass outer = new MethodLocalInnerClass (); outer.outerMethod (); } }
Output:
Inside OuterMethod
Inside InnerMethod
Anonymous Inner Class in Java:
An inner class declared without a class name is known as an anonymous inner class in Java. It is created for implementing an interface or extending a class. Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Its name is decided by the java compiler.
Example to Understand Anonymous Inner Class in Java:
package Demo; abstract class Animal { abstract void dog (); } class AnonymousInnerClass { public static void main (String args[]) { Animal p = new Animal () { void dog () { System.out.println ("Dog is an Animal."); } }; p.dog (); } }
Output: Dog is an Animal.
Static Nested Class in Java:
Static nested classes are not technically inner classes. They are like static members of the outer class. A static nested class is the same as any other top-level class and is nested for only packaging convenience. Because this is static in nature so this type of inner class doesn’t share any special kind of relationship with an instance of the outer class. A static nested class cannot access non-static members of the outer class.
Example to Understand Static Nested Class in Java:
package Demo; public class StaticNestedClass { static class Nested_Demo { public void my_method () { System.out.println ("This is my nested class"); } } public static void main (String args[]) { StaticNestedClass.Nested_Demo nested = new StaticNestedClass.Nested_Demo (); nested.my_method (); } }
Output: This is my nested class
Advantages of Inner Classes in Java:
- It helps in Code Optimization.
- If a class is useful to only one class, it makes sense to keep it nested and together. It helps in the packaging of the classes.
- It has nested classes that are used to develop a more readable and maintainable code.
- The inner classes can access outer class private members and at the same time, we can hide the inner class from the outer world.
- It requires less code to write.
In the next article, I am going to discuss Java Wrapper Classes in detail. Here, in this article, I try to explain the Inner Classes in Java with Examples and I hope you enjoy this Inner Classes in Java with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.