• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Reference and De-reference  RSS feed

 
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question of the day is...

If you de-reference an object is it possible to get that specific object back and re-assign it to that previously used reference? Let's see some code of this happening, shall we? ^_^



As you can see by the end of execution the object that was referenced by firstReference no longer has a reference. This means any instance variables I had stored specifically for that object can no longer be accessed since its reference has been assigned to a different object. Forever gone, or not? Since I was actually fairly partial towards that object I would like to just maybe, maybe someday access its instance variable name again. Is it possible for me to reclaim my lost object, or is it doomed to be consumed by the heartless garbage collector? What cha' got, Ranchers?!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Is it possible for me to reclaim my lost object

No. If you need to use an object again later you need to keep a reference to it. Of course it doesn't have to be the same reference variable. Objects can have multiple reference variables referring to them.

Or you could just add the object to a Collection of some sort

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne is correct, but that is not what dereference means.

Dereference means to take an object reference and use the object it points to. You are implicitly dereferencing an object reference when you write a dot after the name of the reference. What the JVM does is find the object which that reference points to, and apply the dot and whatever follows it, to that object.
I think what you are doing is reassigning the reference.

What makes you think the garbage collector is heartless? Would you like a supply of handkerchiefs?
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Blows Nose* Thanks, Ritchie.

Yes this makes sense and runs smoothly with my thoughts on the topic. The de-referencing makes sense but it's counter intuitive for me since I think of de-referencing as no longer assigning a reference to an object. In effect, you may take that reference and reassign it to another object, as I have done above. In another scenario, how about instead of reassigning that reference to a different object we assign a value of null to that reference. Effectively making the reference not point towards that previous object but now point towards a value of null. Would that still be reassigning, Ritchie?

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:*Blows Nose* Thanks, Ritchie.
In another scenario, how about instead of reassigning that reference to a different object we assign a value of null to that reference. Effectively making the reference not point towards that previous object but now point towards a value of null. Would that still be reassigning, Ritchie?



As far as the object is concerned both scenarios are equivalent. It doesn't matter whether you assign a different object to the variable or assign null to it. Either way, the original object is no longer referenced by the variable, and if that was the only reference to the original object then you can no longer access it. The garbage collector will dispose of it as-and-when it sees fit.

As Joanne mentioned, if you don't want to lose the object then keep a reference to it.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay! And thank you for the input! ^_^
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:*Blows Nose* Thanks, Ritchie.
. . .
You're welcome but the handkerchiefs were for the tears about the garbage collector
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!