4 Types of Java Inner Class – You Must Know!
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Earlier we learned about classes and objects. These are an integral part of programming in Java. However, it is very important to know that classes, just like loops, are nested, i.e one class definition inside another class. The following article will teach you about Java inner class and how to use nested classes effectively.
Inner Classes in Java
Inner classes in Java are the classes that are defined inside the scope of another class. Inner classes are also called nested classes. This helps in easy documentation and better maintenance of the code.
Need for Java Inner Classes
Sometimes you will need to program a class in such a way so that no other class can access it. Hence it would be better if you enclose it within other classes.
If all the class objects are a part of the outer object then it is easier to nest that class inside the outer class. That way all the outer class can access all the objects of the inner class.
Syntax:
public class OuterCLass { //outer code public class innerClass { //Inner class code } }
There are 4 types of inner classes in Java:
1. Nested Inner class
2. Method local inner classes
3. Anonymous inner classes
4. Static nested classes
1. Nested inner class in java
As the name suggests, this type of inner class involves the nesting of a class inside another class. The inner class can access the private variables of the outer class.
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
We can modify access to the inner class by using access modifier keywords such as private, protected, and default. Similarly, access specifiers can help nest interfaces inside one another.
Java program to illustrate the usage of Nested Inner class:
package com.dataflair.innerclass; class OuterClass { public class InnerClass { public void print() { System.out.println("I am printing from the inner class!"); } } } public class NestedInnerClass { public static void main(String[] args) { OuterClass.InnerClass in =new OuterClass().new InnerClass(); in .print(); } }
Output
One thing to remember that even though we can have nested static classes, we cannot have methods that are static inside the nested classes. Because all the methods of the nested class are implicitly connected to the object of its outer enclosing class. Hence they cannot declare any static methods for themselves.
Java program to illustrate the static method in the nested class:
package com.dataflair.innerclass; class OuterClass { public class InnerClass { static public void print() { System.out.println("I am printing from inner class!"); } } } public class StaticInnerClassMethod { public static void main(String[] args) { OuterClass.InnerClass in =new OuterClass().new InnerClass(); in .print(); } }
Output
static public void print()
^
modifier ‘static’ is only allowed in constant variable declarations
1 error
a. Static nested classes in java
If you declare the inner class to be static, then you can access the class without having to create an object of the outer class. However, the static class will not have access to members of the outer class. We can access the elements of the outer class from the inner class.
Java program to illustrate the use of static nested classes:
package com.dataflair.innerclass; class OuterClass { public static class InnerClass { public void print() { System.out.println("I am printing from a static inner class!"); } } } public class StaticInnerClass { public static void main(String[] args) { OuterClass.InnerClass in =new OuterClass.InnerClass(); in .print(); } }
Output
2. Method Local Inner Classes in Java
Up until now, we have seen classes coded inside other classes. In the case of method local inner classes, the outer class method contains the inner class.
However, the inner class cannot use the variables of the outer class if they are not declared as final values. This rule was prevalent until JDK 1.7. After that, inner classes can access non-final local variables.
Java program to illustrate the use of Method Local Inner Classes:
package com.dataflair.innerclass; class OuterClass { void outerClassMethod() { final String site = "Data-Flair.training"; System.out.println("Hey I am inside outerClassMethod"); class InnerClass { void innerClassMethod() { System.out.println("I am studying Java at " + site); } } InnerClass in =new InnerClass(); in .innerClassMethod(); } } public class MainClass { public static void main(String[] args) { OuterClass out = new OuterClass(); out.outerClassMethod(); } }
Output
I am studying Java at Data-Flair.training
Note: If you are using JDK 1.7 or below, removing the final keyword will give you an error like this:
The code inside the inner class is trying to access the local variable named site. The error will be rectified if you declare the variable as final.
3. Anonymous Inner Classes in Java
As the name suggests these inner classes have no name at all. The definition of the classes are written outside the scope of the outer class. These classes are useful when we have to design an interface or overload a method. It saves the effort of us having to nest the class.
They are of two types.
a. Subclass of the specified type.
b. The implementer of the specified interface.
a. Subclass of the specified type
In this method, the anonymous class is put inside a subclass of the outer class. The subclass serves the function as illustrated below.
Java program to illustrate the use of a subclass of a specified type:
class OuterClass { void print() { System.out.println("I am in the print method of superclass"); } } class AnonymousClass { // An anonymous class with OuterClass as base class //start of the anonymous class. static OuterClass out = new OuterClass() { void print() { super.print(); System.out.println("I am in Anonymous class"); } }; public static void main(String[] args) { out.print(); } }
Output
I am in Anonymous class
b. The implementer of specified Interface
The anonymous class can extend a class or implement an interface at a time. We learned about the extending of a class using anonymous classes in the previous example.
Here in this example, we will see interface implementation by anonymous classes. Anonymous classes can also implement interfaces as shown below.
Java program to illustrate the use of anonymous classes to implement interfaces:
package com.dataflar.innerclass; interface Anonym { void print(); } class AnonymousClass { // An anonymous class with OuterClass as base class //start of the anonymous class. static Anonym an = new Anonym() { public void print() { System.out.println("I am an implementation of interface Anonym"); } }; public static void main(String[] args) { an.print(); } }
Output
Summary
We learned about the different types of inner classes and their functions in programming. Inner classes are effective while designing interfaces and classes with intricate definitions. It also helps in organizing similar classes together.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google
Note!!!
Correct this section please or at-least mention the java version which you are using.- “Method Local class cannot access a local variable from outer class until it is defined as final. In the example below, the compiler generates an error.”
From Java 8 all the method local variable which we are using inside method local inner class is bydefault final. So final keyword is not mandatory.