GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes nested member classes and access to parents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "nested member classes and access to parents" Watch "nested member classes and access to parents" New topic
Author

nested member classes and access to parents

Karel KoboojBot
Ranch Hand

Joined: Apr 09, 2004
Posts: 35
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

Joined: Apr 12, 2003
Posts: 425
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.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
can u plz explain what happens here
NonStaticChild parent = NonStaticChild.this;


Thanks,<br />Pallavi
Karel KoboojBot
Ranch Hand

Joined: Apr 09, 2004
Posts: 35

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

Joined: Jan 21, 2004
Posts: 92
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


"Did anyone understand what I have just explained? ... because I did not!"
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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

Joined: Apr 09, 2004
Posts: 35
Thx for the replies, Thomas and Dan. Solution, as always, was pretty simple
 
GeeCON Prague 2014
 
subject: nested member classes and access to parents