1) An object will be garbage collected when it becomes unreachable 2) A reference will be garbage collected if it has null assigned to it 3) The finalize method will be run before an object is garbage collected. 4) Garbage collection assures that a program will never run out of memory.
I think finalize method will not be run always before GC, if it ran before for the same object. Anyone can explain?
finalize() method is only called just prior to garbage collection. It is not called when reference to object is lost. We cannot know when finalize will be executed because we don't know when object will be garbage collected.
One more thing. finalize() method is marked as protected in Object class. When we need it, we override this method. It is probably wrong to call finalize method in your code and it is also probably not to mark it ptotected. But it is guaranteed that finalize() will be called just prior object is garbage collected. But it can happens that finalize() will never be called, if the object is never garbage collected. This is perfectly valid!