Does the garbage collector work on static variables or methods in java?

static fields are associated with the class, not an individual instance.

static fields are cleaned up when the ClassLoader which hold the class unloaded. In many simple programs, that is never.

If you want the fields to be associated with an instances and cleaned up then the instance is cleaned up, make them instance fields, not static ones.

Leave a Comment