Saturday, May 20, 2017
Local inner class in Java
Local inner class in Java
Local inner class is a class which is define local to the outer class. So it must be defined within the local scope of a class like method, constructor or initialization block . A class within a class in never treated as the local inner class. And most importantly scope of the inner class will be limited to where it is declared, outside of which it is not valid. As local inner class is the local scope to the outer class then we all know that within a local scope we cannot define any static member. So here also in the local inner class we cannot define any static member.
For the local inner class a .class file is generated in the following format :
< outer_class > $ < int_value > < inner class > .class
where as int_value depends on the number of times inner class with the same name is used.
Important points regarding Local inner class
1. We cannot define any static member in a local inner class as because of local scope.
2. But static final variable can be used because of its constant nature.
3. In the generated class file of local inner class the int value indicate that the local inner class has been used how many times in a single program with the same name.
4. We cannot use the local inner class with the same name in the same method, constructor or initialization block as like we cannot define local variable with the same name within the same block.
5. As we cannot define any static member so it cannot be the starting point of any java program.
6. We cannot create any object of local inner class outside the scope of that class.
Simple example to show the working of the local inner class :
For the local inner class a .class file is generated in the following format :
< outer_class > $ < int_value > < inner class > .class
where as int_value depends on the number of times inner class with the same name is used.
Important points regarding Local inner class
1. We cannot define any static member in a local inner class as because of local scope.
2. But static final variable can be used because of its constant nature.
3. In the generated class file of local inner class the int value indicate that the local inner class has been used how many times in a single program with the same name.
4. We cannot use the local inner class with the same name in the same method, constructor or initialization block as like we cannot define local variable with the same name within the same block.
5. As we cannot define any static member so it cannot be the starting point of any java program.
6. We cannot create any object of local inner class outside the scope of that class.
Simple example to show the working of the local inner class :
public class prog{
int x; // Outer class instance member
prog(){ //Outer class constructor
class B { // Local inner class
int x; // Local inner class instance member
B(){ // Local inner class constructor
System.out.println("Local inner class");
}
}
B b = new B(); // Local inner class object
class C{} // Local inner class
}
public static void main(String[] args) {
prog p = new prog();
}
}
Available link for download