What is local class in java ?
What is local class in java ?
3.11. Local Classes. A local class is declared locally within a block of Java code, rather than as a member of a class. Typically, a local class is defined within a method, but it can also be defined within a static initializer or instance initializer of a class.
A local inner class is not a member of the enclosing class and hence it can not have any access specifier.
Java Local inner class
A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.
Java local inner class example
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
}
}
A neighborhood class is announced locally inside a square of Java code, instead of as an individual from a class. Normally, a nearby class is characterized inside a strategy, however it can likewise be characterized inside a static initializer or occasion initializer of a class.
Local Classes. A local class is proclaimed locally inside a piece of Java code, as opposed to as an individual from a class. Normally, a local class is characterized inside a strategy, yet it can likewise be characterized inside a static initializer or occurrence initializer of a class.
|
Bookmarks