Variables' name can be changed and shouldn't affect logic. But name() method in Enum returns a constant name as a value so it can break existing code. Should I avoid using name()?
For example,
public enum Example1 {FOO, BAR}
Refactoring FOO name to FOO2 will brake Example1.FOO.name().equals("FOO").
public enum Example2 {
FOO("FOO"),
BAR("BAR");
String code;
private Example2(final String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
In this case, changing FOO name to FOO2 will not brake Example2.FOO.getCode().equals("FOO").
name() directly. Reason: even if the name changes, the semantic (same enum value as before) remains the same.name() is used when serializing/deserializing values. This affects the database (when using the names for O/R mapping), serialized data stored in files or transmitted over the wire (JSON/XML/YAML/... serialization), log entries and more. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With