Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum question

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have got this piece of code which is supposed to work fine, I just cannot figure out why it should.

As you can see there is no valueOf(String) method defined in this enum. Also, the method name() is not defined either. How does it work then? I am pretty confused here.



[ April 19, 2007: Message edited by: Joseph Sweet ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enum types are subclasses of java.lang.Enum.

If you look at the API, you'll see that name() and valueOf() are defined there.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API, you'll see that name() and valueOf() are defined there.


Actually valueOf(String) isn't documented anywhere in the javadocs. I don't know why.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, you're right.

Only the one that takes two arguments in the docs.

The Java Language Specification does show the valueOf(String) method being associated with enum in 8.9 Enums, although it doesn't show up in the API docs or in the source code.
[ April 19, 2007: Message edited by: Keith Lynn ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every enum has two extra static methods: valueOf() and values(). The reason they're not documented in the java.lang.Enum API is because they're static methods of each Enum subclass, not of the Enum class itself. Since they're static, they don't override anything in Enum, and there's no way to declare them abstract in the base class. So there's really no logical place to put such a method in Enum. That's why they're described in the JLS instead.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I can see it.

I think something is wrong if I cannot use the language simply by looking up the API documentation.

Where has the good ole Java gone. Something comprehensible like C++ only portable. Since the 1.5 it's not what it used to be. Mainly due to the generics thing. A language should be a productive tool. Not something that I have to struggle with merely for the purpose of struggling.
[ April 19, 2007: Message edited by: Joseph Sweet ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic