Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing Outer Class variable

 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I thought this will compile and output 78 & 20 but i am getting a compile error saying variable y can't be accessed.
Correct me if i am wrong here, inner class should be able to access any outer class variable..including a private???

TIA
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a bit of a hurry so don't have time to check, but at first sight try Outer.this.y
out is an instance of Outer.Inner; y is a variable of Outer..


Regards,
Ken
 
Looluo Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access y in the body of Inner, not by a reference of Inner.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I was able to get at the value of Outer.y was by adding a method to the Inner class as indicated below:



I then changed the final line to be:

 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the previous post should read as:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic