Friday, May 2, 2008

Genenared methods values and valueOf for Enum

I have recently tried Java Enum when doing software development. The following code is an example:

public Enum FooType {
ABC, DEF;
}

I found two methods valueOf and values in the Javadoc for my enum code. I did not write these 2 methods. And it's superclass java.lang.Enum<FooType> also does not have these 2 methods. Then I checked JLS 3.0. 8.9 Enums says these 2 methods are automatically generated. It is a little magic. One Java class can has methods which do not exist as source code. Too many magic which do not have a consistent rationale can make a language hard to learn and use.

No comments: