-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?

Dan-Cook
  • 98
  • 4
user1369975
  • 1,259
  • 4
  • 15
  • 24
  • 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](https://softwareengineering.stackexchange.com/questions/288715/is-overriding-object-finalize-really-bad). – Neil Sep 17 '18 at 06:39

3 Answers3

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.

Bogdan
  • 3,600
  • 10
  • 13
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.

David Arno
  • 38,972
  • 9
  • 88
  • 121
-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.        
    }
}
Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Bass
  • 1