values( ) and valueOf( ) Methods
All enumerations automatically contain two predefined methods: values( ) and valueOf( ).
Their general forms are:
public static enum-type[ ] values( )
public static enum-type valueOf(String str)
The values( ) method returns an array that contains a list of the enumeration constants. The valueOf( ) method returns the enumeration constant whose value corresponds to the string passed in str.
In both cases, enum-type is the type of the enumeration.
The following program demonstrates the values( ) and valueOf( ) methods:
enum Direction {
East, South, West, North
}
public class Main {
public static void main(String args[]) {
Direction dir;
// use values()
Direction all[] = Direction.values();
for (Direction a : all)
System.out.println(a);
System.out.println();
// use valueOf()
dir = Direction.valueOf("South");
System.out.println(dir);
}
}