• 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 method

 
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys
What happens if I override the finalize method present in the Object class and create a new instance of the object in the finalize method. Sample code given below

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As written, you create a new instance of MyClass, which goes out of scope and is available for garbage collection when the finalize method exits.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a cunning, but pointless, infinite loop, isn't it?

finalize() is a poor mechanism, with few real uses. Not much point getting to involved in it.
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still do not understand the use of this method. If you creating a new object inside the finalize. This object will be available for GC again in this new object GC will call the finalize method which creates another new object. But this goes on util the JVM will exist. So basically the GC is going in loops. for removing one single object. This is my conclusion from this code.

I do not know if this is correct or not. Comments and suggestions are welcome.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Suman Mummaneni

suggestions are welcome

Try it, and tell us what happens.
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well guys here is what happens.
Nothing will happen.
The GC will not go in a loop nor the JVM will crash. As per the rules of GC the program will exit normally.
Here is the link from Java Forums by Sun. that explains it clearly

http://forum.java.sun.com/thread.jspa?threadID=5181278&tstart=15
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic