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)
    // use valueOf()
    dir = Direction.valueOf("South");
  Java Book 
    Language Basics  

  1. enum type
  2. values( ) and valueOf( ) Methods
  3. enum as Class
  4. enum type Inherit Enum
  5. Overriding toString() to return a Token constant's value
  6. Assign a different behavior to each constant.