Are static fields open for garbage collection?

前端 未结 6 1147
谎友^
谎友^ 2020-11-22 13:02

Given an hypothetical utility class that is used only in program setup:

class MyUtils {
   private static MyObject myObject = new MyObject();
   /*package*/s         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-22 13:50

    Static variables are referenced by Class objects which are referenced by ClassLoaders -so unless either the ClassLoader drops the Class somehow (if that's even possible) or the ClassLoader itself becomes eligible for collection (more likely - think of unloading webapps) the static variables (or rather, the objects they reference) won't be collected.

提交回复
热议问题