This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finalize():will garbage collector call finalize() after my code has already called it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finalize():will garbage collector call finalize() after my code has already called it?" Watch "Finalize():will garbage collector call finalize() after my code has already called it?" New topic
Author

Finalize():will garbage collector call finalize() after my code has already called it?

mani manisha
Greenhorn

Joined: Apr 21, 2011
Posts: 8
hi,

In one of the SCJP prep mock exams I saw such a question:
1) code creates an object of a class with finalize method.
2) code calls the finalize method for that object..
3) something makes that object unused and eligible for garbage collection


now, when the GC runs, will it again run the finalize method for the same object..?
(unfortunately no answer or explanation was provided.....)

thanks in advance !




Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

The garbage collector will call the finalize method for a particular object once. It doesn't matter whether you have called the finalize method yourself or not.

So in your case: yes, the garbage collector will call the finalize method.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,
The JVM will call the finalize method only once during the life cycle of object.
Also see the link for further clarification.

Thanks

The important thing is not to stop questioning.Curiosity has its own reason for existing.
mani manisha
Greenhorn

Joined: Apr 21, 2011
Posts: 8
Confusing .. I read in some forum that the JVM will run the finalize method regardless of whether my code called it explicitly or not.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3578
    
  14

The JVM may or may not call the finalize method. If it does, it does it only once. It does this regardless of how many times you call the finalize method yourself. However, you should never call the method yourself.
 
wood burning stoves
 
subject: Finalize():will garbage collector call finalize() after my code has already called it?
 
Similar Threads
Finalize and System.gc()
finalize question
Unicode charector as String literal; finalize() method
Garbage collection will be triggered and finalize method will be called ?
Garbage Collection