This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Override toString() of enum type  RSS feed

 
Ranch Hand
Posts: 36
  • 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: 5485
55
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: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the result is [LOW, TWO, NORMAL, FOUR, HIGH]
 
Sheriff
Posts: 21603
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: 36
  • 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: 62881
203
  • 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: 21603
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: 24089
54
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: 62881
203
  • 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: 10750
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
 
a wee bit from the empire
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!