• 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

Exchange instances of inner classes?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody!
I’m learning work with inner classes now and I discovered a surprised (for me) behavior of inner class.
Please, look at my code.



Looks like instances of inner classes continue to remember they ‘old’ outers. Why so on? Is it possible to say inner that now he has ‘new’ outer?
Thanks for help!

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you expect the state of an object to change because you have reassigned it?

Start:- OuterObj1.innerObj.

The innerObj has a reference to its outer object.

Later:- OuterObj2.innerObj.

Now you replace that inner object shown in red with the previous inner object shown in green

Now: OuterObj2.innerObj.

Just as the object brings its green colour with it, so it brings its OuterObj1 reference with it. So the green inner object still refers to its original outer object.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you expect the state of an object to change because you have reassigned it?

Start:- OuterObj1.innerObj.

The innerObj has a reference to its outer object.

Later:- OuterObj2.innerObj.

Now you replace that inner object shown in red with the previous inner object shown in blue

Now: OuterObj2.innerObj.

Just as the object brings its blue colour with it, so it brings its OuterObj1 reference with it. So the blue inner object still refers to its original outer object.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted twice with different colours in case any readers are colour‑blind.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stal Alexandr wrote: . . . Is it possible to say inner that now he has ‘new’ outer?
Thanks for help!

Probably not, no.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell, I'm red-green colour-blind but I can see the difference between the red and green in your post just fine (but blue and red is easier!).

An instance of a non-static inner class indeed has a link to the instance of its containing class. You can't change that link; you cannot make the instance of the other class have a link to a different instance of its containing class than the one it was created for.

In your example your outer class has a member variable that refers to an instance of an inner class, but this is not the same thing as the link that the instance of the inner class has to its containing class. The member variable of the outer class has nothing to do with this link, so setting the member variable to a different instance of the inner class does not change the link that the instance of the inner class has to the instance of its containing class.

Note that static inner classes do not have a link to an instance of the containing class.
 
Stal Alexandr
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"inner object still refers to its original outer object" and I have no access to change this ref.

Now it is clear for me.

Thanks, Campbell!

 
Stal Alexandr
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Thanks Campbell, I'm red-green colour-blind but I can see the difference between the red and green in your post just fine (but blue and red is easier!).
In your example your outer class has a member variable that refers to an instance of an inner class, but this is not the same thing as the link that the instance of the inner class has to its containing class. The member variable of the outer class has nothing to do with this link, so setting the member variable to a different instance of the inner class does not change the link that the instance of the inner class has to the instance of its containing class.



Yes, Jesper, now I understand it!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Youi're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic