• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Finalize Method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
1) If in finalize method we make re refrence to the object .Will that object ever be garbage collected.
2) Is it mandatory to call the base class finalize method in the subclass's finalize method else the compiler will give compile time error .

 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ms , welcome to javaranch.
PROPER NAMES ARE NOW REQUIRED

Please Refer to the official naming policy of javaranch. You will have to reregister yourself with proper first & last name & help maintain the decorum of the forum.
Waiting for your new posts with proper first & last name

Your Friendly Bartender
Shailesh.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ms !!


1) If in finalize method we make re refrence to the object .Will that object ever be garbage collected.


No. since the gc calls the finalize method only once. If you make any reference to an object then it will not be GC'ed.


2) Is it mandatory to call the base class finalize method in the subclass's finalize method else the compiler will give compile time error .


Compiler won't give any error, but it's neccessory to call the superclass's finalize as finalize methods are not chained by default.
I hope this is clear.
Regards,
Vadiraj. scjp

 
ms
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Vaidraj ,
Actually I read these two questions in one mock test and it was said that that first time the object will not be garbage collected but second time the GC will ignore finalize and the object will be garbage collected .I could not understand the logic .
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ms You it is true that finalize() will called only once on an object.
If your creating a new Instance by using this() in finalize(),that means you r again creating the instance of this type but second time finalize() won't be called.
And also if there is not any refrence which is pointing to the newely object created , it will be again grabage collected.So All the objects which r not represented by any variables are eligible for GC
 
Oh the stink of it! Smell my tiny ad!
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic