Sandra Bachan wrote:Modification to Sierra/Bates, Chapter 3, Question 7.
Please note line 14 where Suits.SPADES automatically prints the name of the enum.
Question 1: Is this an in-built function of the enum?
Question 2: How do I implement toString() such that is prints the name of the Suits object in all lower-case. I attempted this in line 9, but in vain.
AFAIK
1) Suits.SPADES is an Enum Constant.
An Enum constant is internally a a final static reference of the Enum Type Suits.
When Suits.SPADES is referred to in the function System.out.println, toString() function of this reference will be invoked.
Default toString() function of the Enum class invokes the name() function of the Enum class which returns the constant name as declared in the enum.
2) Replacing toString() function in this code to the code below would give you the desired result:
public String toString(){ return name().toLowerCase();}
This invokes the same name() function as mentioned above followed by toLowerCase() function of the String class.