tag:blogger.com,1999:blog-28228406.post4557294691216625448..comments2024-03-27T00:44:17.190-04:00Comments on Java How To ...: Custom String Values for EnumUnknownnoreply@blogger.comBlogger12125tag:blogger.com,1999:blog-28228406.post-20607017440423921902009-08-04T07:46:18.481-04:002009-08-04T07:46:18.481-04:00It's a valid example of use if, for example yo...It's a valid example of use if, for example you wanted to do something more complicated thatn just capitalisation, eg adding a space then another word to every value...Blaisenoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-33901111228268107892009-06-12T00:12:29.005-04:002009-06-12T00:12:29.005-04:00Fletch, I just happened to stumble upon this again...Fletch, I just happened to stumble upon this again. I guess I am just used to using capitals for enum identifiers.<br />... no, not because Sun says so, but because my lecturers did :P<br /><br />To be honest though, it does help their values stand out. (to me)<br /><br />Also, I posted my comment because I thought people might find it handy. <br />Obviously it's a simple solution, but you never know...Phazonaddictnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-45043072905677010902009-05-06T09:21:00.000-04:002009-05-06T09:21:00.000-04:00public String toString() {
if (this.name == null)
...public String toString() {<br />if (this.name == null)<br />return super.toString();<br /> <br /> return name;<br />}Aneesh Vijendranhttps://www.blogger.com/profile/02407345627270555120noreply@blogger.comtag:blogger.com,1999:blog-28228406.post-61587176206263373592009-04-23T19:24:00.000-04:002009-04-23T19:24:00.000-04:00public enum Type
{
NORMAL, FIRE, WATER, THUNDER,...public enum Type<br />{<br /> NORMAL, FIRE, WATER, THUNDER, ICE,<br /> GRASS, GHOST, DARK, GROUND, STEEL,<br /> WIND, PSYCHIC, POISON, MAGIC, ROCK,<br /> FIGHTING, LIGHT, COSMIC, RAINBOW;<br /> <br /> public String toString()<br /> {<br /> String output = name().toString(); // Example: NORMAL<br /> output = output.charAt(0) + output.substring(1).toLowerCase();<br /> <br /> return output; // Output: Normal<br /> }<br />}<br /><br /><br />From a program I'm writing, this should allow both the value and the string to work fine.Phazonaddictnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-91604280914206794922009-03-02T09:21:00.000-05:002009-03-02T09:21:00.000-05:00thank you for the post and for the comment #2. Bot...thank you for the post and for the comment #2. Both were very good!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-51468802368355531902008-07-04T09:48:00.000-04:002008-07-04T09:48:00.000-04:00if you want the round trip enumstring you can use ...if you want the round trip enum<->string you can use the valueOf()/name() combination.<BR/><BR/>The name method returns the exact string identifier for that enum value.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-88810586572063685992008-04-11T12:05:00.000-04:002008-04-11T12:05:00.000-04:00Good point. valueOf(String name) only takes the id...Good point. valueOf(String name) only takes the <B>identifier name</B>, not the toString() value.javahowtohttps://www.blogger.com/profile/18276646603642614642noreply@blogger.comtag:blogger.com,1999:blog-28228406.post-80121601689534090722008-04-01T12:10:00.000-04:002008-04-01T12:10:00.000-04:00Note that all these examples will break the round-...Note that all these examples will break the round-trip enum<->string as you cannot change the behavior of the static method Enum.valueOf().<BR/><BR/>Xyz.toString () may give you the desired output, but Enum.valueOf () will not.<BR/><BR/>Of course, if you don't need to turn a string back into an enum, or you can guarantee all callers will use your class' impl of valueOf, you're golden.<BR/><BR/>It really is a shame that ordinal value cannot be explicitly defined.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-73367575083355690502008-03-23T09:50:00.000-04:002008-03-23T09:50:00.000-04:00The second comment gives a more compact implementa...The second comment gives a more compact implementation of custom string value (see his/her example enum Xyz). And in most cases, it's a better approach. The advantage of the approach described in the blog post is it allows each element to customize its display, at a more fine-grained level. If we were to do it at the whole enum level, we would have to conditionize it, which is not very OO.javahowtohttps://www.blogger.com/profile/18276646603642614642noreply@blogger.comtag:blogger.com,1999:blog-28228406.post-41604425364822365292007-09-27T08:20:00.000-04:002007-09-27T08:20:00.000-04:00Why not use override toString for the whole enum?p...Why not use override toString for the whole enum?<BR/><BR/>public enum ABC {<BR/><BR/>Y, N;<BR/><BR/>@Override<BR/>public String toString() {<BR/> if (this == Y) return "yes";<BR/> else return "no";<BR/>}<BR/>}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-70798651621724724272007-06-10T13:00:00.000-04:002007-06-10T13:00:00.000-04:00I believe a better approach would have been to pro...I believe a better approach would have been to provide a constructor for your enumeration:<BR/><BR/>public enum Xyz {<BR/>A("Hello"), B("Goodbye");<BR/>private Xyz(String name) {<BR/> this.name = name;<BR/>}<BR/>private final String name;<BR/>public String toString() {<BR/> return name;<BR/>}<BR/>}<BR/><BR/>In fact you can give your constructors any number of parameters of any type and supply additional methods on your enumerations to access those values.<BR/><BR/>This also prevents the anonymous inner class issue you noted.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-28228406.post-54323505919540596142007-05-15T12:38:00.000-04:002007-05-15T12:38:00.000-04:00Thanks, i was just looking for this since my naive...Thanks, i was just looking for this since my naive approach (overriding toString in the scope of the enum) didn't work!Anonymousnoreply@blogger.com