File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 3361
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finalize():will garbage collector call finalize() after my code has already called it?
 
Similar Threads
Garbage Collection
Garbage collection will be triggered and finalize method will be called ?
finalize question
Finalize and System.gc()
Unicode charector as String literal; finalize() method