Lew wrote :
// do not change as it is persisted!
private static final int MY_ENUM_ID = 1;
public enum IsEnum
{
MY_ENUM(MY_ENUM_ID);
private int id;
private IsEnum( int databaseID )
{
id = databaseID;
}
public int getDatabaseID()
{
return id;
}
}
and then, of course, some code to scan the enums and find the id you
are looking for.
This is safe against the Java compiler setting ordinals and renaming
enums.
But it is not safe against the programmer redefining 'MY_ENUM_ID',
which is the same degree of change as altering the enum constant name
or an assigned string name. It's equivalent to the suggestion
upthread to add 'fromString()' and override 'tostring()' to match,
with a 'String' identifier instead of an 'int', and has the same
degree of safety (danger).
All you really show in your example is substitution of one constant,
the 'int', for another, the 'enum' value. Enums are a type-safe
replacement for compile-time constants such as 'MY_ENUM_ID'; using
them together like that is redundant.
And buys nothing.