• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Garbge collector !!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have found this question from mock exam
is it true or false ?
Any class that includes a finalize method is not necessary
to invoke super class�s finalize() method.
whereever i think it should be true
but the ans. is false
can anybody pl.tell me the right answer
thanks in adv.
sunil
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not mandatory to call super.finalize() in the derived
class.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But most likely it is necessary. The purpose of a finalize() method is to free up any system resources that are tied to the object. If you override a superclasses finalize to do something (probably cute), and you DON'T call the super's finalize, the stuff that IT frees up will never happen.
It's your choice, but think it through well.
From the JLS 12.6 Finalization of Class Instances


The fact that class Object declares a finalize method means that the finalize method for any class can always invoke the finalize method for its superclass, which is usually good practice. (Unlike constructors, finalizers do not automatically invoke the finalizer for the superclass; such an invocation must be coded explicitly.)

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sunil kathuria:
i have found this question from mock exam
is it true or false ?
Any class that includes a finalize method is not necessary
to invoke super class�s finalize() method.
whereever i think it should be true
but the ans. is false
can anybody pl.tell me the right answer
thanks in adv.
sunil



yes, superclass's finalise meyhod is always invoked first, if you don't provide this, it will automatically be invoked by JVM.
If I am wrong please correct me.
Regards
Jyotsna
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jyotsna,
You are incorrect. Please read the above quote from the JLS.
finalizers do NOT automatically invoke the finalizer for the superclass
The answer to the original question is FALSE, you DO need to call the super's finalize.
[This message has been edited by Cindy Glass (edited June 08, 2001).]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is a tricky question. If you don't call the superclass' finalizer it won't be done for you. BUT you're not required to make this call (although it's certainly encouraged). So perhaps the answer is true - it's not strictly necessary.
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Cindy for correcting me but one doubt remains there.
If you override a superclasses finalize to do something ,and you DON'T call the super's finalize, the stuff that IT frees up will never happen. I mean what are the implications if you don't call super's finalize?

Jyotsna
[This message has been edited by Jyotsna Umesh (edited June 08, 2001).]
[This message has been edited by Jyotsna Umesh (edited June 08, 2001).]
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finalize() method will be called by JVM anyway before the objects are actually destroyed. To explicitly call this method is the programmer's choice, not to call it is programmer's choice too.
Of course, when you decide to call an object's finalize() method, you actually want to free its associated system resources. If you've defined a finalize() method in its superclass of course you want to invoke it too. However if you don't, or if you haven't extend any class ( except Object class via implicit construct), then there is no need for you to invoke super.finalize(). Invoking super.finalize() in this case just do nothing more than that by the system.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the super class finalize DOES do something usefull, like freeing up IO resources, and you put a finalize in the subclass and do NOT call the super's finalize, then those IO resources will NEVER be released until the JVM goes down (like you turn off your PC or something).
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Even I think this is a very tricky question.
According to me , still the answer is true.
This is because :
when you explictly call finalize(), it would be just like calling any other method and will not release the system resources.
If you don't explicitly call finalize() the JVM would call the finalize() when it needs to release system resources of that object.
When you call finalize with the intention of releasing system resources and not with the intention of just a method, only if your call super.finalize() the system resources would be released for that object.
However, if just finalize() is called and its super finalize() is not invoked, i think , it will be just like any other method and the object will be finalized by the JVM.
So , the conclusion according to me is that there is no need to call super.finalize().
Please correct me if i am wrong
Thanks in advance
padmini
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Any class that includes a finalize method is not necessary
to invoke super class�s finalize() method.
whereever i think it should be true
but the ans. is false


i think the stress here is on the word necessary ... and because of this i think the answer is <>false<>. I think what the word 'necessary' implies would probably be something like if we would get a compile time error if we do not call the super classes finalize() method
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
sunil kathuria
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
-----------------------------------------------------------------
The fact that class Object declares a finalize method means
that the finalize method for any class can always invoke
the finalize method for its superclass, which is usually
good practice. (Unlike constructors, finalizers do not
automatically invoke the finalizer for the superclass;
such an invocation must be coded explicitly.)
---------------------------------------------------------------
hi cindy,
according to above quote ,wherever i understand that
it is a convension to call super class finalize method
but it is not mandatory .
the lines are stating that it is not a similiar concept
of constructor chaining where if we donot define super(),
the compiler will automatically call super class
constructor.in finalize method if we wantto call super
class finalize method then we have to call explicitly.
and according to me the answer shuld be true
pl. correct me if i am wrong.
sunil
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers!!
I think the question is vague or incomplete:

is it true or false ?
Any class that includes a finalize method is not necessary
to invoke super class�s finalize() method.

The above state tell us it is not necessary to invoke super class' finalize() method is "True" because it did'nt state that to free the memory or something else. So in current situation if we don't want to free the memory and use finalize() method unnecessarily, then the answer is true but if we want to free some memory or use finalization process then false because then we want super finalize to call.
Regards "Logic is better than marks"
Farhan
 
reply
    Bookmark Topic Watch Topic
  • New Topic