• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nested member classes and access to parents

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u plz explain what happens here
NonStaticChild parent = NonStaticChild.this;
 
Karel KoboojBot
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for the replies, Thomas and Dan. Solution, as always, was pretty simple
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic