• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classes

 
N Goldsmith
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't an inner class get at the this ptr for the surrounding class?

class a{
int x;
class b{
x=5;
}
}

If b has access to a's methods and variables, why can't it also get at a's this ptr somehow w/out having to explicitly pass it in to b's constructor?
 
Matt Fielder
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and ptr is code for?...
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ptr = pointer (C++ hacker language)

The answer is because inner class is an object by itself, and "this" refers to the inner class object. Do you need to access methods of your outer class from the inner class? You can always have the outer class pass it's own reference to the inner class
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try "a.this.x = 5;"

I assume the above statement will be inside a method or non-static initializer of class b. I also assume that "x" is an instance variable, not a local variable.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic