wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing var in innerclasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing var in innerclasses" Watch "Accessing var in innerclasses" New topic
Author

Accessing var in innerclasses

Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
Hi All,

Is there any way to access the variable var of temp from inner2 context. Or How can I assign value 5 to var2 through var of its outer classes?

class temp {
private static int count;
private int var=5;
class inner1{
private int var=6;
class inner2{
private int var2=var;
{
System.out.println("In inner2::"+var2);
}
};
};
public void method() throws Exception{
inner1 in1=new inner1();
inner1.inner2 in =in1.new inner2();

}

}
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
First of all you are suggested to use code tags whenever you have some code to discuss.

This code may help you:


The trick is: private int var2=temp.this.var;
Hope you got it.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Bharathi Kongara
Ranch Hand

Joined: Sep 15, 2005
Posts: 56
Simple, you just need to access your outer class's variable like this temp.this.var; That is your assignment will change like
private int var2 = temp.this.var; It will print 5!!!

Bharathi Kongara
Ranch Hand

Joined: Sep 15, 2005
Posts: 56
Well, Sandeep posted before me, but then we both wanted to tell the same!!!
Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
Hi,

Thank you for the reply. I understood how to access.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing var in innerclasses