aspose file tools*
The moose likes Beginning Java and the fly likes HeadFirstJava 2nd Edition Page 58 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HeadFirstJava 2nd Edition Page 58" Watch "HeadFirstJava 2nd Edition Page 58" New topic
Author

HeadFirstJava 2nd Edition Page 58

Venkateshwar Rao Eturi
Greenhorn

Joined: May 24, 2008
Posts: 3
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: May 24, 2008
Posts: 3
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: May 24, 2008
Posts: 3
Thank you allvery much for the help.

Venkatcute
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You're welcome
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HeadFirstJava 2nd Edition Page 58