| 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: 3050
|
|
|
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.
|
 |
 |
|
|
subject: Finalize():will garbage collector call finalize() after my code has already called it?
|
|
|