What's important to understand is that enums are referenced by name.
Let's say we have an enum:
package com.example;
public enum MyEnum {
ONE, TWO, THREE
}
And a simple test class:
package com.example;
public class EnumTest {
public static void main(String[] args) {
System.out.println(MyEnum.TWO);
}
}
Now let's decompile the test class using the javap command line tool:
javap -verbose -c com/example/EnumTest
yields this (truncated for brevity):
Compiled from "EnumTest.java"
...
const #22 = Field #23.#25; // com/example/MyEnum.TWO:Lcom/example/MyEnum;
const #23 = class #24; // com/example/MyEnum
const #24 = Asciz com/example/MyEnum;
const #25 = NameAndType #26:#27;// TWO:Lcom/example/MyEnum;
const #26 = Asciz TWO;
const #27 = Asciz Lcom/example/MyEnum;;
...
{
public static void main(java.lang.String[]);
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #16; //Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #22; //Field com/example/MyEnum.TWO:Lcom/example/MyEnum;
6: invokevirtual #28; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
9: return
}
Notice that this code references the constant #22
which in turn indirectly references #26
which is the ASCII string "TWO".
So as long as the class name of MyEnum
stays the same and the name of the instance TWO
is not changed one does not have to recompile class EnumTest
.