How is a Java static class garbage collected?
I have a static class, which contains a static object. If I set the object to null then it is garbage collected. I don’t understand the lifecycle of a static class. Is it garbage collected when the program closes?
Think of this way:
Every object has a reference to its class.
Every class has a reference to its class loader.
Every class loader has a reference to every class it has ever loaded.
It is possible that this whole ball of mess becomes garbage. This is the basis for app unloading/reloading (in a servlet container). However if there’s even one object still referenced, the class loader and all its classes cannot be unloaded, therefore it is not an easy task to achieve app unloading.