• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Override toString() of enum type

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i don't undrestand why it print [LOW, TW O, NORMAL, FOUR, HIGH] as result?
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What result are you wanting?

Interesting code there. I didn't think you could override toString() for each constant.
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the result is [LOW, TWO, NORMAL, FOUR, HIGH]
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

emma roberts wrote:i don't undrestand why it print [LOW, TW O, NORMAL, FOUR, HIGH] as result?


Then what did you expect? For enums, the default toString() implementation returns the same as name(), which is the name you give the constant. For three of them you've overridden toString(), and the overidden result is what you're getting for those.

Carey Brown wrote:I didn't think you could override toString() for each constant.


It's unusual, but any non-final method can be overridden in the enum constant. That excludes methods like order(), name(), equals() and hashCode(), but not toString(). What you're effectively creating is an anonymous sub class of the enum type - the only possible way to extend an enum. This sub class follows all normal rules.

This possibility to sub classing is also why you normally shouldn't use getClass() on an enum constant but getDeclaringClass(). From it's javadoc:

Returns the Class object corresponding to this enum constant's enum type. Two enum constants e1 and e2 are of the same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass(). (The value returned by this method may differ from the one returned by the Object.getClass() method for enum constants with constant-specific class bodies.)

 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
therefore the toString() method of each constant enum is automatically executed  when Priority.values() is executed
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

emma roberts wrote:therefore the toString() method of each constant enum is automatically executed  when Priority.values() is executed

No, it is called by Arrays#toString().
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell is right. Priority.values() only returns an array of Priority, it doesn't call any instance methods.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I didn't think you could override toString() for each constant.



Sure, like Rob said you can override pretty much anything. You can even declare a method of the enum and then override it in each constant. I've done that. I have an enum which represents levels in a (biological) taxonomy and it has a method called canBeChildOf(the enum); each enum constant overrides that method to specify what other enum constants it can be a child of in the tree.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . . You can even declare a method of the enum and then override it in each constant. . . . .

There is an example about arithmetic in the Java™ Language Specificatiaon showing you how to do that.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check Rob's first reply. Commas don't work the way you (plainly) think they do, so you haven't overridden toString() for TWO and FOUR.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic