Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner classes" Watch "inner classes" New topic
Author

inner classes

N Goldsmith
Greenhorn

Joined: May 18, 2004
Posts: 22
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

Joined: Oct 27, 2004
Posts: 158
...and ptr is code for?...
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
 
Don't get me started about those stupid light bulbs.
 
subject: inner classes
 
jQuery in Action, 3rd edition