aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Name of enum object in all lower-case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Name of enum object in all lower-case" Watch "Name of enum object in all lower-case" New topic
Author

Name of enum object in all lower-case

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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.



Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Have you checked the API for java.lang.Enum for something that might help you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

Sandra Bachan wrote:Just looked at it.

And you saw nothing that might be helpful?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

BTW, where is your tolower() method?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
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.


Preparing for SCJP
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@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

Joined: Jan 10, 2002
Posts: 61437
    
  67



"Practice makes perfect" which is why I sent you to the API instead of just blurting out the answer.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 05, 2010
Posts: 2
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

Nothing "special"... it acts just like any other inner class.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 05, 2010
Posts: 2
You're right thanks.
Not sure why i didn't think of that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Name of enum object in all lower-case