GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes finalize method call Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finalize method call Error" Watch "finalize method call Error" New topic
Author

finalize method call Error

RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18

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 .
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 13, 2010
Posts: 18
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 13, 2010
Posts: 18
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

Joined: Jan 28, 2008
Posts: 5575

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?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

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.

Dhruva
 
GeeCON Prague 2014
 
subject: finalize method call Error