1

I know that the garbage collector of Java can tidy up the dead class instances, but how about the class itself? I mean the static fields of a class?

I know that during the class initialization, static fields get initialized, but after this, is there a way to delete them?

Gab是好人
  • 1,976
  • 1
  • 25
  • 39

1 Answers1

4

A Class can be GCd if its ClassLoader is GCd.

After that you will have to reload the class, at which point the static fields will be re-initialized.

This can be easily tested by loading a class with for example a URLClassloader.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2
    Note that from jdk 7+ even String literals defined inside a class (which go into the String constants pool) can get GCed if classloader of that class is GCed. – TheLostMind Aug 13 '15 at 13:07
  • Test: `static final Object x = new Object() {{ System.out.println("Good Morning, Groundhog" }};` – Joop Eggen Aug 13 '15 at 13:10