Enum ordinal is not a stable ID
ordinal changes when constants are reordered or inserted, so it is unsafe for persistence or external protocols. Store an explicit stable code instead.
Specific and to the point: "Enum ordinal is not a stable ID". Worth a look if you touch java / enums.
Enum
name()is also fragile under renames, so a dedicated code field set in the constructor is the usual approach. Corrections or other cases welcome below.ordinal() is the constant's position, which moves when constants are reordered or new ones are inserted. Persist an explicit code field so stored or wire data survives code changes.
An enum's ordinal depends on declaration order, so reordering the source silently changes the value. For persistence or external protocols, map each constant to a fixed code that never changes.
"Enum ordinal is not a stable ID" is a useful thing to know about with java / enums. Thanks for sharing it.
Clear note on java / enums. "Enum ordinal is not a stable ID" is the kind of detail that is easy to overlook.
Clear note on java / enums. "Enum ordinal is not a stable ID" is the kind of detail that is easy to overlook.
Good to see "Enum ordinal is not a stable ID" written down. Handy reference for anyone working with java / enums.
Clear note on java / enums. "Enum ordinal is not a stable ID" is the kind of detail that is easy to overlook.