Hi All,
As far as i know, the finalize() method is called before the object is destroyed by the automatic garbage collector.
My doubt is, if an object is not reachable then will it immediately execute the finalize() method?
If so, then why the below code is not printing the message in finalize() method after line //2 is executed.
public class GarbageCollect
{
String id;
GarbageCollect(String objid)
{
id = objid;
}
public static void main(String[] args)
{
GarbageCollect obj1 = new GarbageCollect("one"); //line 1
obj1 = new GarbageCollect("two"); //line 2
}
protected void finalize() throws Throwable
{
System.out.println(id + "Garbage Collected");
super.finalize();
}
}
Please correct me if my understanding is wrong.
Thanks in advance!!
Regards,
Surekha.