Life span of a Java enum instance -


i understand there 1 single instance per predefined constant of enum type. right? now, suppose there instance variable called value of enum, , suppose one predefined constant. if value modified (the) instance of one affects variables referring one. happens value of value when garbage collector gets rid of instances of enum? ever thrown away even?

example

here simple example there 2 variables a , b, both referring one. value of value negated before b used first time. if garbage collector had gotten rid of instance of one before line number b = number.one;, value of value have been "reset" 1, presume. correct?

the enum:

public enum number {     one(1), two(2), three(3);       int value;      number(int value) { this.value = value; }      void negate() { value = -value; }      @override     public string tostring() { return "value: " + value; } } 

the main method:

public static void main(string[] args) {     number = number.one;     a.negate();     number b = number.one;      system.out.println(a + "   " + b); } 

output:

value: -1 value: -1

so question is, can output ever following (if a = null; added before b used)?

value: -1 value: 1

enums implicitly public static final fields, never garbage collected.

edit

as kindly pointed out in comments @realskeptic, things little bit more complex in jvm instance multiple class loaders. static field values may disapear when classes unloaded.

however, default, there 1 class loader alive long jvm instance alive, in example application can never happen static fields garbage collected.


Comments