• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explicitly invoking finalize()

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've gone through the K&B Java 6 study guide in preparation for the certification exam. It appears to me that the discussion of the finalize() method on the practice exam CD is different than what's stated in the book. Can someone please clarify for me whether or not the finalize() method can really only be invoked once on an object if the class has an overriding finalize() method that gets used?

According to page 263 of the study guide: if an object becomes eligible for garbage collection after an explicit call to finalize(), the garbage collector will not run finalize() again.

What's confusing me is the explanation for one of the practice exam questions that states: "Even though finalize() was invoked explicitly for that object, the JVM can invoke finalize() one more time for that object....."

These statements seem to be contradicting each other. Am I missing something? Please help!!
 
Greenhorn
Posts: 16
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Audrey,

What is mentioned in study guide is correct.

and,

GC will collect a object memory only when the object does't have any reference for a time.
Finalize() method gets executed before the object is eligible for GC.
In case in the finalize method execution if the object may get a reference to live, then GC won't collect that object.

After some times if the object become eligible for GC again. Whether the finalize method gets called again or not? this is what given in you practice Exam.



Thanks,
Rajasekar.
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Audrey Williams wrote:Can someone please clarify for me whether or not the finalize() method can really only be invoked once on an object if the class has an overriding finalize() method that gets used?



The explanation for the practice exam question is correct: the JVM will certainly call finalize() no more than once for that object (at a time not governed by contract). However, in deciding whether to do so, the JVM pays no attention to whether finalize() was explicitly invoked for the object.

Here's what the language spec says:

An unfinalized object has never had its finalizer automatically invoked; a finalized object has had its finalizer automatically invoked. A finalizable object has never had its finalizer automatically invoked.

...the finalize method is never automatically invoked more than once by a Java virtual machine for each object, even if the object is again made reachable after it has been finalized.

Explicit invocation of a finalizer ignores the current state of the object and does not change the state of the object from unfinalized or finalizable to finalized.



 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, an optimization here would make sense.

Hmm....

Nope-- it appears to hit finalize twice, in keeping with the spec:

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object#finalize method is a call back method of JVM. if you call the method explicitly, it is just like any other method call.
 
Audrey Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for all of your responses. This has really helped me tremendously.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic