File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested member classes and access to parents

 
Karel KoboojBot
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given this little piece of code:

It's easy to retrieve the directly enclosing class of the inner class, but is it possible to retrieve the enclosing grandparent class (the instance of the Parent class) with one expression ? The expression in comments certainly gives a compile time error.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm unsure what you are trying to achieve here.
If you were hoping to access instance variables or methods of the Parent class than have a look at the following code. Remember that a member class can access members of the Pareant class. You don't need any reference to the Parent or grandparent.
 
pallavi utukuri
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u plz explain what happens here
NonStaticChild parent = NonStaticChild.this;
 
Karel KoboojBot
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can u plz explain what happens here
NonStaticChild parent = NonStaticChild.this;

Every object of a member inner class always contains a reference to its enclosing class. The expression above can be used to retrieve that enclosing object.
My question was the following: is it possible to retrieve the enclosing grandparent class in a similar fashion? I realise that you can call methods of those enclosing classes without a problem, but was just wondering about the possibility of retrieving the grandparent.
 
Dan Andrei
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try Parent par =Parent.this;
for any enclosing class of an inner class you can use the above access to get to its instance.
for example:
Class A1{
class A2{
class A3{
...
class An{
void moo{
A1 a1=A1.this;
A2 a2=A2.this;
A3 a3=A3.this;
///....
An an=An.this;//OR
An an=this;
}
}//end An
}//end A3
}// end A2
}//end A1
hope that makes sense
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write:

To fully test that we have a reference to the grandparent you can try the following code:

[ April 19, 2004: Message edited by: Thomas De Vos ]
 
Karel KoboojBot
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for the replies, Thomas and Dan. Solution, as always, was pretty simple
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic