-2

If we have a code snippet like:

public class Finalizer{
    static List list = new ArrayList();

    public void finalize( ){
        System.out.println("hello");
    }
}

What is so special about static here that affects the call to finalize ( ). Is it because that static members have lifecycle of whole program so a variable referring to the Finalizer object won't be out of scope because of static unless we say null on it?

  • Don't override finalize if you can help it. Except for some edge cases, it is probably not being used the way it ought to be used. More info here. – Neil Sep 17 '18 at 6:39
4

There is nothing special about static members here. The finalize method is called on an object when the object is being garbage collected. An object is being garbage collected when the object is no longer reachable from any part of the application.

The only difference is that static members are reachable for a longer period of time because of the way they are referenced. A static member is referenced by a class while the class is referenced by its class loader. Most class loaders have a long life time (the bootstrap class loader lives as long as the JVM is running) and rarely become eligible for Garbage collection, so for this reason the static members are still referenced long after any instances of that class have been garbage collected.

So the list in your example is not tied to instances of your Finalizer class, it is tied to the Finalizer class itself. The field and instances of the class are garbage collected separately, the static does not affect the call to finalize for the instances of the class.

One other thing to be aware of in case you didn't know, finalizers might never run if the garbage collector never runs. If you have a lot of memory and a small short lived application, the garbage collector might not bother to reclaim any memory before your application exists.

3

What is so special about static here that affects the call to finalize ( )

The two are completely unrelated and do not affect each other.

Is it because that static members have lifecycle of whole program so a variable referring to the Finalizer object won't be out of scope because of static unless we say null on it?

No, static members are completely unconnected with object instances. Having list as a static member will not stop instances of Finalizer being garbage collected if required.

-1

Nothing really, but it gets more interesting if you have the code below, as every instance of Finalizer will be held in memory for the entire lifespan of the program, without being garbage collected.

public class Finalizer{
    static List list = new ArrayList();
    public void finalize( ){
        list.add(this); // Do not try this.        
    }
}

protected by gnat Sep 11 '18 at 20:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.