• 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 call Error

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
I am trying to understand how finalize() method call works so i tried to override the method and i called Sytem.gc() method but to my surprise the finalize() method is not getting executed i tried to run the program multiple times but I could find the finalize() method executed can anyone please help me in finding the answer


I tried to understand what is actually the problem i found some conclusions but i am not sure of that .
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you test


the thing is that when ever a object is going to Garbage Collection just the *finalize method of the Object* runs. In your case the finalize method of Finalizedemo wont run. instead String object's finalize method will run. however java.lang.String doest noe override finalize, which means Object's finalize method[which contains nothing to print] will run.

also below code is the good example of overriding a finalize method.


Hope this helps

 
RajTilak Sivaluri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:Now you test
. instead String object's finalize method will run. however java.lang.String doest noe override finalize, which means Object's finalize method[which contains nothing to print] will run.



Thank you for the response

Even i thought this might happen but my doubt is exactly here when i call System.gc() function it has to internally call finallize() method but why is that the string class finalize method getting executed .
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RajTilak Sivaluri wrote: why is that the string class finalize method getting executed .



because, the object being garbage collected is a String object.
 
RajTilak Sivaluri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

because, the object being garbage collected is a String object.



Yup my doubt is that in the program flow gc() has finalize method in it but why id finalize method of string class called because in this case string class is not child class but still why string class finalize() method called
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RajTilak Sivaluri wrote:my doubt is that in the program flow gc() has finalize method in it


I dont know what you are talking about?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, RajTilak!

Yup my doubt is that in the program flow gc() has finalize method in it but why id finalize method of string class called because in this case string class is not child class but still why string class finalize() method called



Champ, the thing is that the Object class provides a finalize() method, which does nothing. If we want something to be done, we have to override it according to our needs, just like you did. This method is (or may be) called by the VM when the object is being garbage collected, so you can have an opportunity to do clean ups, release resources, etc. Therefore, the finalize() method of all objects that are about to be garbage collected have their finalize() methods called when the GC is running.
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello RajTilaK,

Yup my doubt is that in the program flow gc() has finalize method in it but why id finalize method of string class called because in this case string class is not child class but still why string class finalize() method called



best way to call finalize() is with try-catch block, where any exception is assumed to handle, along with finally block if externally you wish to call finalize()of object.

String class doesnt have any finalize() method and, there is no guarantee of forced call to gc() runs GC - it depends upon JVM only


so, if JVM calls GC, finalize() gets called automatically but here, in your code, there is no way to call the overriden method within the class so it is not getting called.
reply
    Bookmark Topic Watch Topic
  • New Topic