This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is syntax to get container object of instance of inner class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "What is syntax to get container object of instance of inner class?" Watch "What is syntax to get container object of instance of inner class?" New topic
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
    
  13

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is syntax to get container object of instance of inner class?
 
Similar Threads
question about inner class instantiation
All Objects always extend "Object"
inner class question
Whats the use of inner class?
Inner Class