Scott A. Hightower wrote:
"Mike Schilling" <mscottschilling@hotmail.com> wrote in message
news:hr83q3$228$1@news.eternal-september.org...
Scott A. Hightower wrote:
Given a reference to an inner class instance, is there a direct way
to refer to its enclosing class instance?
I can do it indirectly by returning a qualified "this" from a method
of the inner class:
class Outer {
int i;
/* ... */
class Inner {
Outer getMom() { return Outer.this; }
}
}
class X {
void bump(Outer.Inner y) {
Outer z = y.getMom();
// Outer z = what without getMom()?
z.i++;
}
}
I'm fairly sure the answer is "no". In fact, I don't think that,
without using reflection, it would be possible for X to determine
whether an Inner even has an enclosing instance (that is, whether
it's an inner class or a nested class.)
(My apologies to Mike - I hit the wrong button and sent this directly
to him.)
In this particular case, the programmer knows that Inner has an
enclosing instance, knows the class name for that instance and knows
that the fields of interest are accessible.
I was just wondering. The getMom() method does the trick. Just
seemed a klunky way to get it.
But darned if I can think of what kind of syntax the expression would
have.
Yeah, that was my point. If there were syntax for such a thing, it should
of any such animal.