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.