CAN SOMEONE PLEASE EXPLAIN WHAT SHOULD BE THE CORRECT ANSWERS TO THE FOLLOWING THANKS! Which of the following statements about Java's garbage collection are true? a) The garbage collector can be invoked explicitly using a Runtime object. b) The finalize method is always called before an object is garbage collected. c) Any class that includes a finalize method should invoke its superclass' finalize method. d) Garbage collection behavior is very predictable
Harpal Singh
Ranch Hand
Joined: Oct 10, 2000
Posts: 229
posted
0
a) The garbage collector can be invoked explicitly using a Runtime object. above is a contradictory statement allthough you can call system.gc() but there is no gurantee that Garbage collector will be called.... b) The finalize method is always called before an object is garbage collected. b) is true c) Any class that includes a finalize method should invoke its superclass' finalize method. c is false d) Garbage collection behavior is very predictable d is very much false ......GC is unpredictable...it is JVM dependent.... Thanks , Harpal
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Sorry Harpal ... I need to quibble a.) True. You can explicitly call the garbage collector; however, it won't necessarily run. b.) agree, it's true c.) True. While you don't have to call the superclasses finalize method; it is considered good programming practice, therefore you should call it.
... finalize invokes super.finalize ... Train yourself so that you always do so in any finalize method you write. If you don't invoke super.finalize, you may correctly finalize your own part of the object, but the superclass's part will not get finalized. Java Programming Language: Second Edition p 49
d.) agree, it's false ------------------ Jane The cure for boredom is curiosity. There is no cure for curiousity. -- Dorothy Parker [This message has been edited by Jane Griscti (edited November 02, 2000).] [This message has been edited by Jane Griscti (edited November 02, 2000).]
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2547
posted
0
b) The finalize method is always called before an object is garbage collected. This looks true but actually may not be! When an object is garbage collected the finalize method will be called. But if in finalize() we ressuruct the object what will happen? JLS says that finalize() is called ONLY ONCE during the life time of the object. Does it mean : 1. The GC will never try to collect this object again? (Implication is this object will live for ever!) 2. The GC will try to collect it but will not call the finalize() again. This mean, you just can't resurruct an object after one try! Ie. you actully can't save any object permanently!