Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Name of enum object in all lower-case

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked the API for java.lang.Enum for something that might help you?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just looked at it.

Even though it advises AGAINST overwriting toString(), I would like to know how to do it anyway (to study for the SCJP exam).

Would like it such that the name of the Suits object prints lower-case.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra Bachan wrote:Just looked at it.

And you saw nothing that might be helpful?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, where is your tolower() method?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear: I'm still looking at the API, trying to figure out anything that may help

@Abimaran: tolower() is on line 9
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra, Abimaran is asking where have you declared tolower method in your enum. Its not a built in method for enums. There are 10 methods in java.lang.Enum class, one of them returns the name of the enum i.e. if we declare our enum as SPADE, that method will return "SPADE", you can convert it to lower case once you have the enum name as a String...
 
Vicky Mehta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Vicky: I updated the code and it now works.




@ All: Guess I need to practice UNDERSTANDING an API :-)


Thanks!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


"Practice makes perfect" which is why I sent you to the API instead of just blurting out the answer.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Sandra, Abimaran is asking where have you declared tolower method in your enum. Its not a built in method for enums. There are 10 methods in java.lang.Enum class, one of them returns the name of the enum i.e. if we declare our enum as SPADE, that method will return "SPADE", you can convert it to lower case once you have the enum name as a String...


Oh! Sorry, I misunderstood the question!
 
John Avila
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i have a question, it sure does seem like

System.out.println(Suits.SPADES + " " + Suits.SPADES.points);

shouldn't be accessible because it is private.

I suppose this is because enums are "special" and is part of the bridge class?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing "special"... it acts just like any other inner class.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Myname Is wrote:
shouldn't be accessible because it is private.

I suppose this is because enums are "special" and is part of the bridge class?


Try this...

 
John Avila
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right thanks.
Not sure why i didn't think of that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic