Inner Class
Inner classes are class within Class. Inner class instance has special relationship with Outer class. This special relationship gives inner class access to member of outer class as if they are the part of outer class.
Note: Inner class instance has access to all member of the outer class(Public, Private & Protected)
Syntax for creating Inner Class
1 2 3 4 5 6 |
//outer class class OuterClass { //inner class class InnerClass { } } |
Type of Inner class
- Static
- Method Local
- Anonymous
- Other then above these normal inner class
Normal Inner Class
Inner classes which are not method local , static or anonymous are normal inner class.
1 2 3 4 5 6 |
//outer class class OuterClass { //inner class class InnerClass { } } |
If you compile above code it will produce two class file.
1 2 |
outer.class inner$outer.class |
Note: You can’t directly execute the inner class’s .class file with java command.
As it is not static inner class so we can’t use static keyword with it.
How to access Inner Class
Inner class can be accessed only through live instance of outer class.
Within Outer Class
Outer class can create instance of the inner class in the same way as normal class member.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class OuterClass { private int i = 9; // Creating instance of inner class and calling inner class function public void createInner() { InnerClass i1 = new InnerClass(); i1.getValue(); } // inner class declarataion class InnerClass { public void getValue() { // accessing private variable from outer class System.out.println("value of i -" + i); } } } |
From Outside Outer Class
Create outer class instance and then inner class instance.
1 2 3 4 5 6 7 8 9 10 11 |
class MainClass { public static void main(String[] args) { // Creating outer class instance OuterClass outerclass = new OuterClass(); // Creating inner class instance OuterClass.InnerClass innerclass = outerclass.new InnerClass(); // Classing inner class method innerclass.getValue(); } } |
Above code can also be replaced with
1 |
OuterClass.InnerClass innerClass = new OuterClass.new InnerClass(); |
this keyword
There are some rules associated with this and it refer the currently executing Object. So in case of Inner class “this” keyword will refer the currently executing inner class Object. But to get this for outer class use “OuterClass.this”.
Modifiers Applied
Normal inner class will be treated like member of the outer class so it can have several Modifiers as opposed to Class.
- final
- abstract
- public
- private
- protected
- strictfp
Note: Don’t get confused with the modifiers of Class and Inner Class. They are completely different.
Method Local Inner Class
When an inner class is defined inside the method of Outer Class it becomes Method local inner class.
Syntax for Creating Method Local Inner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class OuterClass { private int i = 9; // Creating instance of inner class and calling inner class function public void innerMethod() { // inner class declarataion inside method class InnerClass { public void getValue() { // accessing private variable from outer class System.out.println("value of i -" + i); } } //inner class instance creation InnerClass i1 = new InnerClass(); i1.getValue(); } } |
Now definition of inner class is inside a method in Outer class. Still the instance of the outer class can be created but only after definition of inner class as you can see above.
Note:
- Method local inner class can be instantiated within the method where it is defined and no where else.
- Method local inner class can not use the variable defined in method where it id defined still it can use the instance variable.
- If method local variable is “Final” method local inner class can use it.(* Now variable is Final)
Modifiers Applied to Method Local Inner Class
Method local inner classes are eligible for modifiers like local variable so an method local inner class can have final or abstract.