• Post Reply Bookmark Topic Watch Topic
  • New Topic

GC : finalize()  RSS feed

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the below statements regarding GC true or false
1. finalize() can be overloaded.
2. The finalize() method can throw any exception in the throws clause
3. If an object is garbage collected then the finalize method
is guaranteed to be called.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi angela,
3 is true.2 is also true becoz the signature of this method is
protected void finalize() throws Throwable
1 is also true.but then this overloaded method would not be the one which would be called during the gc.
this is what i think.correct me if i am wrong.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjella,


1. finalize() can be overloaded. // true BUT be careful to
in calling the super.finalize() explicitely in the overloaded function, as in the overloaded method the finalize() method of Object class will not be called implicitely.
2. The finalize() method can throw any exception in the throws clause // true it can throw anything that are subclass of "Throwable", the reason is that <code>finalise()</code> method of Object class declares that it can throw Throwable object. Please note Throwable is the superclass of all the "exceptions" and "errors".
3. If an object is garbage collected then the finalize method
is guaranteed to be called. // false. Sorry there is no guarantee in here.

Hope your querry is answered now.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 11, 2001).]
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I feel all three options are correct, with one condition; i.e., before GC is done finalize will be called unless otherwise the finalize had already been called and the object is resurrected.
Java guru's please correct me if I am wrong..
Thanks in advance
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Ravindra,
Thanks for the answers. But i have some queries.
1. What is the specific reason behind overloading the finalize()
method. I mean what purpose will it serve ?
2. W.r.t to the 3.) statement, i agree to some point. becoz we donot
when exactly the object will be garbage collected (or not ) by GC,
but before it does so i suppose the finalize() will be called,
to perform any leftover tasks, like closing File etc.So i have
a bit confusion over this one as to why it is false.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjella,
The catch is that it is not guaranteed because, JVM does not guarantee that it call the finalize() method on the object as it may at times try to optimise. It can only
be "suggested", there are lot of grey areas
pertaing this point.
Hope this is clear now.
Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 11, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!