Java – unload static fields

I have a Java class that uses complex static fields and requires special operations as close () so that they can be safely cleaned up by GC

For static field initialization, I use static blocks But I do not know how to uninstall the static domain safely, so I can call the close () method before GC cleanup the field.

Is there a way to unload static fields, similar to static initialization blocks?

Solution

There is no way to do what you require, because static blocks are initialized when loading classes, and finalize () only applies to objects

Consider replacing static variables and complex operations with a singleton class and an instance of it

In this way, you can use the finalize () method to perform the close () operation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
***
下一篇>>