If you look in the enum api at the method name() it says that:
Returns the name of this enum constant, exactly as declared in its enum declaration.
name() is literally the textual name in the java code of the enum. That means it is limited to strings that can actually appear in your java code, but not all desirable strings are expressible in code. For example, you may need a string that begins with a number. name() will never be able to obtain that string for you.
It really depends on what you want to do with the returned value:
name()
as toString
may have been overridentoString
which may have been overriden (or not!).When I feel that it might be confusing, I provide a more specific getXXX
method, for example:
public enum Fields {
LAST_NAME("Last Name"), FIRST_NAME("First Name");
private final String fieldDescription;
private Fields(String value) {
fieldDescription = value;
}
public String getFieldDescription() {
return fieldDescription;
}
}
A practical example when name() and toString() make sense to be different is a pattern where single-valued enum is used to define a singleton. It looks surprisingly at first but makes a lot of sense:
enum SingletonComponent {
INSTANCE(/*..configuration...*/);
/* ...behavior... */
@Override
String toString() {
return "SingletonComponent"; // better than default "INSTANCE"
}
}
In such case:
SingletonComponent myComponent = SingletonComponent.INSTANCE;
assertThat(myComponent.name()).isEqualTo("INSTANCE"); // blah
assertThat(myComponent.toString()).isEqualTo("SingletonComponent"); // better
name()
is a "built-in" method of enum
. It is final and you cannot change its implementation. It returns the name of enum constant as it is written, e.g. in upper case, without spaces etc.
Compare MOBILE_PHONE_NUMBER
and Mobile phone number
. Which version is more readable? I believe the second one. This is the difference: name()
always returns MOBILE_PHONE_NUMBER
, toString()
may be overriden to return Mobile phone number
.
Use name()
when you want to make a comparison or use the hardcoded value for some internal use in your code.
Use toString()
when you want to present information to a user (including a developper looking at a log). Never rely in your code on toString()
giving a specific value. Never test it against a specific string. If your code breaks when someone correctly changes the toString()
return, then it was already broken.
From the javadoc (emphasis mine) :
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
You can also use something like the code below. I used lombok to avoid writing some of the boilerplate codes for getters and constructor.
@AllArgsConstructor
@Getter
public enum RetroDeviceStatus {
DELIVERED(0,"Delivered"),
ACCEPTED(1, "Accepted"),
REJECTED(2, "Rejected"),
REPAIRED(3, "Repaired");
private final Integer value;
private final String stringValue;
@Override
public String toString() {
return this.stringValue;
}
}