In java can we create a inner class inside a method??
Answer to this query is yes .
Example code :
class Outer
{
String outerVar="outer Variable";
public void checkInner()
{
final String localVar="Local Variable";
class Inner
{
public void check()
{
Outer o2=new Outer();
System.out.println("outer var ::"+outerVar);
System.out.println("local var ::"+localVar);
}
}
Inner i1=new Inner();
i1.check();
}
}
public class NestedClass {
public static void main(String args[])
{
Outer o1=new Outer();
o1.checkInner();
}
}
In the above code Outer class is the enclosing class and Inner class is the non static nested class which is defined inside a checkInner() method of the Outer class.
0 Comment(s)