Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Questions on Finalizer Guardian Idiom

  Asked By: Aditi    Date: Mar 20    Category: Java    Views: 1781
  

I am a newbie on Java. I need your help on a puzzle on java's
finalizer.There is an item on finalizer, "Avoid finalizers", in Joshua
Bloch's famous book "Effective Java". He gave an idiom called "Finalizer
Guardian" to make sure the superclass's finalizer to be performed whenever
derivedclass's finalizer is called. here is the code:

// Finalizer Guardian idiom
public class Foo {
// Sole purpose of this object is to finalize outer Foo object
private final Object finalizerGuardian = new Object() {
protected void finalize() throws Throwable {
// Finalize outer Foo object
...
}
};
... // Remainder omitted
}

My quesition is how this idiom can assure the Foo's annonymous inner
instance's finalizer will be performed when the Foo's derived instance's
finalizer is called by the GC. When GC calls a object's finalizer, Will it
definetely call the member's finalizer?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Bhanu Chidigam     Answered On: Dec 17

The Garbage collector(GC) while cleans up the object it also calls the finalize method of the respective class.
When a class derived is derived base class and if only instance for the derived class exists, and if it also miss the call to finalize version of based class from the derived finalize version. Then no way GC can call the finalize method of the base class.
As a work around this technique ensures to call clean up code for the base class. An anonymous class object as member of base class is declared, and when GC cleans up this object it calls finalize version of the anonymous version of finalize. where supposed be clean up code for the base class (GC while propagate to this anonymous object while derived class object is cleaning up).



 
Didn't find what you were looking for? Find more on Questions on Finalizer Guardian Idiom Or get search suggestion and latest updates.




Tagged: