Overriding Enum#toString not desirable?

enums, java

Solution

I think there is simply some confusion regarding the meaning of the word "desirable" here. The javadoc basically says the same thing twice: overriding toString() is generally not required for the enum to work and it's therefore not useful (desirable) to do so.

So to answer your question: it would not be desirable to override toString() if: A) you know that you'll never have to display a string representation of the enum's name, or B) the default string representation suffices to identify the enum should this be required.

Problem

I just noticed that the `Enum#toString` javadoc states (emphasis mine): Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum type should override this method when a more "programmer-friendly" string form exists. By default, `toString()` and `name()` return the same thing, so even once `toString` has been overriden, one can still access the name of the enum through the `name()` method. Does anybody know why overriding `Enum#toString` would not be desirable? EDIT: For reference, `name()`'s javadoc (emphasis as in the original): Returns the name of this enum constant, exactly as declared in its enum declaration. Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name. This method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release.

Original source