• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

finalize() method.(Plz.clarify the doubt)

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
What is correct?
1.A class extending a class having a finalize() method MUST write a finalize that calls super.finalize().
OR
2.We can do without writing the finalize() method.
OR
3.We can override the finalize() method of our super class.
Please clarify the doubts as I am totally confused.
Thanks in advance.
Regards,
Kavita.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavita,
The answer would seem to be "it depends on what you want to perform.".
The first choice is incorrect because Object has a finalize method and I have written millions of classes that extend Object and don't have a finalize method.
Therefore, following the above logic, choice 2 looks good. If we are not interested in reclaiming any extra space we have used (i.e., file referencs, etc.) then this choice would do.
Choice 3 is also correct! We can always override a superclass method. If we do though we should at least call super.finalize() because whoever wrote the superclass thought it was important to reclaim some memory and you should honor that by calling it. However, as stated above, the JVM will not insist on it!
Regards,
Manfred.
[This message has been edited by Manfred Leonhardt (edited April 11, 2001).]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both 2) and 3) are right.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavita,
Your question
What is correct?
1.A class extending a class having a finalize() method MUST write a finalize that calls super.finalize().
OR
2.We can do without writing the finalize() method.
OR
3.We can override the finalize() method of our super class
Options 2 and 3 are correct.
Option 1 is wrong because of word MUST.
Normally called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. This is normal method so we don�t having chaining of finalize() method like constructors. You can call super.finalize() but it is not MUST
Option 2 is correct because
JVM is not enforcing any rule that finalize method has to performs some special action; it simply used to perform cleanup actions before the object is irrevocably discarded. If object is not having finalize method then it will get one from �Object� which performs no special action; it simply returns normally.
Option 3 is correct because
finalize()is a protected instance method of Object class. There is nothing special about it. As you can override any protected methods in subclass, you can override this method too. Remember it is protected so you can�t override with private modifier. It also throws Throwable object. The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates. Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Regards
Vinod
 
Kavita Ghia
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My doubt is clarified.
MANY THANKS TO ALL FOR THE REPLY.
 
And inside of my fortune cookie was this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic