Question 4 Which of the following classes should have a custom finalize method? a A class that creates a socket. b All classes. c Any class that extends a class having a finalize method. d Any class that creates an array of primitive values. Ansers are A, C While I don't have problem with A. But isn't C suggesting that any class that is extended from Object should also declare finalize method becuase Object declares finalaize method?
Yes, I think you are logically correct. But (from the API):
The finalize method of class Object performs no special action; it simply returns normally.
I would have interpreted that option to have meant that some intermediate base class had provided a non trivial implementation of finalize, so I would have chosen C. But why? Surely the finalize method is called polymorphically through a reference to an Object? So the lowest finalize in the hierachy will be called. Provided that finalize called its super.finalize() method, there is no need to provide an implementation in every subclass all the way down the hierachy. For example, if we have a hierachy: Object <- A <- B <- C <- D, and B implements finalize. Then B must call super.finalize(). C and D do not need to implement finalize. So choice C looks not correct, but not for the reason Sarma proposed. Bill? I found this description (near the end ) [ March 15, 2003: Message edited by: Barry Gaunt ]
I am not convinced that one should have a custom finalize() method in a class that creates a socket. I understand that finalize() can be used to clean up non-memory resources used by an object. And socket() uses a non-memory resource. However� Read Item #6: Avoid finalizers in Effective Java by Joshua Bloch. The Java Programming Language says �You should only rarely need to write a finalize() method�. I�ll bet people use socket() frequently rather than rarely. In Java Network Programming by Harold (O�Reilly) there is no discussion on finalize(). In Java RMI by Grosso (O�Reilly) there is no discussion on finalize(). In Core Java II there is no discussion on finalize(). I searched the Sun Java Forums and found no advice on using finalize() when using socket(). What I gleaned from scanning the Sun Java Forums for use of the word "finalize" is that nowadays people recommend using weak references and advise against using finalize(). I am tempted to post a question on the Sun Java Advanced Features forum and ask whether people who do socket() calls for a living ever use finalize(). [ March 16, 2003: Message edited by: Marlene Miller ] [ March 16, 2003: Message edited by: Marlene Miller ]
What I was trying to get at with that question is the need to write a finalize for classes that use resources which are not cleaned up normally by the GC process. I think an object that may have an open socket is a good candidate, but I am open to other suggestions. Bill
Thank you William for your insight on this question. I noticed when I was searching the Sun Java forums for discussions on finalize that some of the participants with a lot of Java knowledge and experience do not understand finalize is used to clean up non-memory resources (as opposed to memory resources). This is a good point to test. I also noticed when searching the Sun Java Technical reports and Effective Java, an even more important thing to know about finalize is that an explicit termination method (such as close) used in a try-finally block is better than finalize. Finalize is a safety net in case the owner of the object forgets to call the explicit termination method. My only suggestion or request is that the correct answer to a question agrees with the real world.
Howdy, this is a great discussion, but I just wanted to point out that you will not have a question like this on the real exam. You *can* expect questions about how/when finalize() works, but not about whether it's a good idea cheers, Kathy