What is Local inner class ?

Class declared inside a method is known as local inner class.
It can not be declared as private,public or protected.
to use the inner class you must make an instance
of it somewhere within the method.

Inside main():

class demo
public static void main(String[] args){
class x {int j=10;}
System.out.println(new x().j);
Inside our own method:
class demo
void fun(){
class x {int j=10;}
x obj=new x();//this line must come after the class
public static void main(String[] args){
demo d=new demo();
- You can't mark a method-local inner class public, private, protected, static, transient,
and the like

Key Points:
❑ A method-local inner class is defined within a method of the enclosing class.
❑ For the inner class to be used, you must instantiate it, and that instantiation
must happen within the same method, but after the class definition code.
❑ A method-local inner class cannot use variables declared within the method
(including parameters) unless those variables are marked final.
❑ The only modifiers you can apply to a method-local inner class are abstract
and final. (Never both at the same time, though.)

No comments:

Post a Comment