• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !




 
Greenhorn
Posts: 24
Android Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic