Home arrow static arrow Java Programming [Archive] - Accessing outer class data member from the inner class
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Accessing outer class data member from the inner class
This topic has 2 replies on 1 page.

Posts:36
Registered: 5/25/04
Accessing outer class data member from the inner class  
Jul 1, 2004 11:27 AM



 
Hi,
I have a class within class (inner class) and i need to know how can i use/set a data memeber that is declared in the outer class from within the inner class.

Thanks
 

Posts:441
Registered: 2/25/04
Re: Accessing outer class data member from the inner class  
Jul 1, 2004 11:45 AM (reply 1 of 2)



 
class Outer {  int member;  class Inner {    void method () {       member = 7;    }  }}
Or
class Outer {  int member;  class Inner {    int member; // hides outer member    void method () {       Outer.this.member = 7; // use explicit Outer.this to get to outer member    }  }}
 

Posts:4,680
Registered: 6/14/99
Re: Accessing outer class data member from the inner class  
Jul 1, 2004 11:46 AM (reply 2 of 2)



 
Try it and see ... unless it's an inner class within a method of the outer class, or it's a top level (static) class it pretty much the same as with the outer class ... example:
class bb {  private int i = 10;  class cc {    cc() {      System.out.println(i);    }  }  public static void main(String[] argv) {    bb.cc bc = new bb().new cc();  }}
 
This topic has 2 replies on 1 page.