Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Enum question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum question" Watch "Enum question" New topic
Author

Enum question

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
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 ]

We must know, we will know. -- David Hilbert
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Enum question