This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Override toString() of enum type  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i don't undrestand why it print [LOW, TW O, NORMAL, FOUR, HIGH] as result?
 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the result is [LOW, TWO, NORMAL, FOUR, HIGH]
 
Sheriff
Posts: 21686
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
therefore the toString() method of each constant enum is automatically executed  when Priority.values() is executed
 
Marshal
Posts: 63781
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 21686
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is right. Priority.values() only returns an array of Priority, it doesn't call any instance methods.
 
Sheriff
Posts: 24282
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!