finalize method call Error

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 .

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.

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 .

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

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.

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.