• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HeadFirstJava 2nd Edition Page 58

 
Venkateshwar Rao Eturi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am a new member. On page 58, there is one abandoned object and one null reference. Is it possible to refer the null reference to refer to the just abandoned object, in this case Book Object 1 ?

Thanks,

Venkatcute
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please post more details; many of us don't have that particular book and don't know what is on page 58, so more details will mean more chance of a helpful reply.

CR
 
Gavin Ralston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example involves creating two objects with initially one reference to each:



Then, in step two, switching the reference in b to a copy of c:



Then a third example of changing c to null:



If I'm understanding the question correctly, you can't go back and have c reference Object 1.

Object 1 was abandoned in step two. Since you have no reference variable in existence which remembers how to get back to Object 1, it's no longer accessible.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a trick you can do with finalize() in which you assign the about-to-be-GC'd object to a reachable variable. So it's conceivable that you can get an object back even after no variable references it.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
There's a trick you can do with finalize() in which you assign the about-to-be-GC'd object to a reachable variable. So it's conceivable that you can get an object back even after no variable references it.
But that's only a party trick which we don't use in real life.

To all intents and purposes, Gavin is correct.
 
Venkateshwar Rao Eturi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you allvery much for the help.

Venkatcute
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
There's a trick you can do with finalize() in which you assign the about-to-be-GC'd object to a reachable variable. So it's conceivable that you can get an object back even after no variable references it.

It's also a trick that you don't want to use ever. finalize() is only called zero or one times on any object. If you use that finalize() to "save" the object, the next time the GC will claim it finalize() is 100% surely NOT called.

finalize() is for cleaning up any resources still kept, not saving objects that should be removed.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I certainly wouldn't advocate trying to use such a parlor trick. I'm just pointing out it's conceivable, because as soon as you say something "can't" be done...
[ May 26, 2008: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic