Java 1.5 Enumerations

Published: Wednesday, 2 November 2005

Sample Code

public class Issue {
     public enum Status {OPEN, CLOSED};

     private Status status;
     private String issueName;
}

The above class represents an Issue, where its status can either be OPEN or CLOSED.

To loop through all possible statuses

for (Issue.Status s : Issue.Status.values()) {
    System.out.println(s);
}

The statuses will automatically be converted into strings.

OPEN
CLOSED