Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing var in innerclasses

 
Roja Rani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bharathi Kongara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Sandeep posted before me, but then we both wanted to tell the same!!!
 
Roja Rani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for the reply. I understood how to access.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic