Hi Everybody, This is a program from Thinking in Java, Bruce Eckel.The author says "If any portions of the object are not properly cleaned up, then you have a bug in your program that could be very difficult to find. The value of finalize( ) is that it can be used to discover this condition, even if it isn�t always called." Here, he is using finalize to check the state of instance member checkedOut .If its true he finds it as an erroneous condition,and prints out a message regarding it.And since second object(see line# 1) ,never calls checkIn(), and therefore checkedOut never necomes false ,for this instance.Now my questions is: 1)Firstly, am I correct with my understanding of the program? 2)if yes,then,whether setting checkedOut to false, which is the default value for boolean instance variables, makes the object properly cleaned up,and thus an ideal situation for the garbage collector to work.Also consider this explanation of the author "Note that System.gc( ) is used to force finalization (and you should do this during program development to speed debugging). But even if it isn�t,it�s highly probable that the errant Book will eventually be discovered through repeated executions of the program (assuming the program allocates enough storage to cause the garbage collector to execute)."
Helo! I do not think that cant help you on this but I have tried to raise questions on finalize on http://www.javaranch.com/ubb/Forum24/HTML/011374.html and I would like to see somebody to come up and explain this. ------------------ Antti Barck It Solutions Consultant -- NSD Oy Sun Certified Programmer for the Java� 2 Platform
In regards to the original code snippet, I find your interpretation in point 1 correct as far as my understanding of the system goes.
The author seems to be using the finalize method for an internal post-condition check of sorts as I see it. He's looking to see if he "lost" any books while they were checked out. It's interesting, but given the stages in finalize in the VMs, I'm not sure that the System.gc() will <em>always</em> force the finalize method on objects to be called. It's not a guarantee. The spec actually reads "best effort". There is also, of note, the method System.runFinalization() which will run all objects in the state of cleanup whose finalizers have not executed.
I'm not a VM guru to speak on the reliability of this method, but it seems to me it would work, but may or may not be definitive according to the specification. Perhaps someone closer to the problems inherent in the various VMs could comment?
Just recalling my latest Java University session with Peter Haggar who pointed me to this board in the first place, and his session on <em>Advanced Java Programming Language Techniques</em> states in Chapter 6 --
"... Do NOT rely on finalization() methods at all
- They are not guaranteed to execute
- Objects are not guaranteed to be freed
I'd call Peter a definite authority on the subject. His book
Practical Java is on my list of "to buy" and likely deals with the subject in some depth as well.