Inner class means one class which is a member of another class. There are basically four types of inner classes in java.
1) Nested Inner class
2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
Nested Inner class can access any private instance variable of outer class. Like any other instance variable, we can have access modifier private, protected, public and default modifier.
Like class, interface can also be nested and can have access specifiers.
Following example demonstrates a nested class.
In a nested class method
As a side note, we can’t have static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself. For example the following program doesn’t compile.
Error illegal static declaration in inner class Outer.Inner public static void main(String args) modifier ‘static’ is only allowed in constant variable declaration
An interface can also be nested and nested interfaces have some interesting properties. We will be covering nested interfaces in the next post.
Method Local inner classes
Inner class can be declared within a method of an outer class. In the following example, Inner is an inner class in outerMethod().
Inside outerMethod Inside innerMethod
Method Local inner classes can’t use local variable of outer method until that local variable is not declared as final. For example, the following code generates compiler error (Note that x is not final in outerMethod() and innerMethod() tries to access it)
local variable x is accessed from within inner class; needs to be declared final
Note : Local inner class cannot access non-final local variable till JDK 1.7. Since JDK 1.8, it is possible to access the non-final local variable in method local inner class.
But the following code compiles and runs fine (Note that x is final this time)
Inside outerMethod X = 98
The main reason we need to declare a local variable as a final is that local variable lives on stack till method is on the stack but there might be a case the object of inner class still lives on the heap.
Method local inner class can’t be marked as private, protected, static and transient but can be marked as abstract and final, but not both at the same time.
Static nested classes
Static nested classes are not technically an inner class. They are like a static member of outer class.
inside inner class Method inside outerMethod
Anonymous inner classes
Anonymous inner classes are declared without any name at all. They are created in two ways.
a) As subclass of specified type
i am in show method of super class i am in Flavor1Demo class
In the above code, we have two class Demo and Flavor1Demo. Here demo act as super class and anonymous class acts as a subclass, both classes have a method show(). In anonymous class show() method is overridden.
a) As implementer of the specified interface
i am in anonymous class
In above code we create an object of anonymous inner class but this anonymous inner class is an implementer of the interface Hello. Any anonymous inner class can implement only one interface at one time. It can either extend a class or implement interface at a time.
This article is contributed by Pawan Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java