previous | start | next

## Typesafe Enumerations

- Class with fixed number of instances
  public class Size
  {
  private Size(String name) { this.name = name; }
  private String name;
  public static final Size SMALL = new Size("SMALL");
  public static final Size MEDIUM = new Size("MEDIUM");
  public static final Size LARGE = new Size("LARGE");
  }
- Private constructor!
- String field not necessary (but convenient for toString, serialization)
- Typical use:
  Size imageSize = Size.MEDIUM;
  if (imageSize == Size.SMALL) . . .

previous | start | next