• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
mani manisha
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Android Java Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
Hibernate Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The JVM will call the finalize method only once during the life cycle of object.
Also see the link for further clarification.

Thanks
 
mani manisha
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic