what is Non-static inner class?

The nested class is not static.
A regularnon static inner class can't have static declarations of any kind. The only way you can access the inner class is through a live instance of the outer class!

Calling the non static class variables in the same class:

class demo
{
class x
{int i=10;}

public static void main(String[] args){
demo d=new demo();
demo.x obj=d.new x();
//demo.x obj=new demo().new x();//it can replace above two lines
System.out.println(obj.i);
//System.out.println(new demo().new x().i);//we can call like this also
}};
Calling non static inner class variables in other class:
class demo1
{
class x
{int i=10;}
};
class demo
{
public static void main(String[] args){
demo1 d1=new demo1();
demo1.x obj=d1.new x();
System.out.println(obj.i);
//System.out.println(new demo1().new x().i);//we can do like this also
}};

No comments:

Post a Comment