| Author |
What is syntax to get container object of instance of inner class?
|
John Robertson
Greenhorn
Joined: Feb 12, 2007
Posts: 7
|
|
I can not seem to find the syntax for referring to the container object of an instance of an inner class. For example class Outer { class Inner { public boolean hasSameContainer( Outer.Inner input ); } } If the method hasSameContainer is supposed to determine whether the container object of "this", which is "Outer.this", was the same as the container object of "input", what syntax is to be used to refer to the container object of "input"? The basic idea is that I have inner classes which can be combined by various methods, but only if they belong to the same container object, not otherwise. John Robertson [ August 06, 2007: Message edited by: John Robertson ]
|
 |
Andy Grove
Greenhorn
Joined: Nov 11, 2003
Posts: 18
|
|
How about storing the container as a member variable of your Inner class? Is this what you want to do? public class Outer { class Inner { protected Outer container; public Inner(Outer container) { this.container = container; } public boolean hasSameContainer(Inner i) { return this.container == i.container; } } }
|
 |
John Robertson
Greenhorn
Joined: Feb 12, 2007
Posts: 7
|
|
Well, I could do that, but since there is a syntax to get the container object of "this", I assume there is also a syntax to refer to the container object of an instance of an inner class other than "this". I am trying to find out what that syntax is. Thank you for the suggestion though. John Robertson
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Originally posted by Andy Grove: How about storing the container as a member variable of your Inner class?
But it is already stored as such; this would be redundant. There's actually no special syntax for this at all! One way to do it:
|
[Jess in Action][AskingGoodQuestions]
|
 |
John Robertson
Greenhorn
Joined: Feb 12, 2007
Posts: 7
|
|
Well, I am surprised there isn't a syntax for that, it seems like there ought to be. On the other hand, given there is no syntax for it I think your solution is quite elegant. -John Robertson
|
 |
 |
|
|
subject: What is syntax to get container object of instance of inner class?
|
|
|