wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize method" Watch "finalize method" New topic
Author

finalize method

praneeth kumar
Greenhorn

Joined: May 10, 2007
Posts: 20
Hello Everyone

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?


SCJP 1.5 - 91%
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
yes even than finalize method is called only once


http://subodh-gupta.blogspot.com
praneeth kumar
Greenhorn

Joined: May 10, 2007
Posts: 20
Thanks a lot for the reply.

Is there any way to check this practically?

Thank You.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
i dont have the code but their are refrences(phantom refrence etc) which can be obtained before the object is GC for some proccessing need to be done before the object is destroyed you can try this.

And this is the reason finalize is not called twice it'll turn up into an infinite loop.

object destroy -- finalize call --- obtain refrence----do something --- destroy object -- finalize call --- so on.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169

praneeth kumar

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?



if you invoke finalize method explicitly on any object, finalize will be invoked then also on that object by JVM. But JVM invokse only once on that object. If you try to resurrect that object through finalize, and when again that object becomes elligible for garbage collection, this time JVM wont invoke finalize() on it.

here is a sample code::



somehow System.gc() did perform garbage collection (atleast on my system) and the O/P I got::

E:\Priyam>java Test
102010

Though I have not resurrected the object, but you can try your self...

[ July 23, 2007: Message edited by: Priyam Srivastava ]

"History would be kind to me, for I intend to write it."
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers!

Praneeth asked:
Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?


No it is different.
And yes, the finalizer daemon may invoke it again.

The GC will call finalize only once (Maximum. Maybe never.). How many times you invoked finalize directly does not matter.
Try to design some example code to explore.



Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize method