Friday, February 27, 2009

access static and instance variables of outer class from inner class



package innerclass;
//To access static and instance variables of outer class from inner class
public class Outer {
int x=10;
static int y=20;
class Inner
{
int l=30;
public void m1()
{
System.out.println(x+"..."+y);
}
}
public static void main(String ar[])
{
Outerc o=new Outerc();
Outerc.Inner in=o.new Inner(); //Outer.Inner in=new Outer().new Inner();
in.m1();
//System.out.println(in.x+"..."+in.y);//error ( x and y are not instance variables of inner class)
System.out.println(in.l);

}
}

output:
10...20
30




0 comments:

Post a Comment