I have a java class that uses complex static fields which need special operations as close() so that they are safely cleaned by GC.
For the initialization of static fields I use the static block. But I don’t now how to unload the static field safely, so that I can call the close() method before the field is cleaned up by GC.
Is there any way to unload a static field, similar to the static initialization block?
There isn’t a way to do what you are asking because the static block gets initialized when the class is loaded and finalize() only works on objects.
Consider replacing your static variables and the complex operation in it with a Singleton class and an instance of it.
This way, you can use a
finalize()method to perform yourclose()actions.