Enumeration

Jan 06 2010

Java Enumerations

Enumerations

Per Sun's Java documentation, a Java enum "is a type whose fields consist of a fixed set of constants ... you should use enum types any time you need to represent a fixed set of constants."

  • Prior to Java 1.5, there were 2 basic ways to define new types: classes and interfaces
  • In some cases neither one of the options was sufficient, especially for a finite set of a specific type of data
  • Example of some finite sets include grades, planets, compass directions, countries, genders, and ethnicity types
  • These types of construct were possible prior to 1.5 but required a lot of work and were prone to issues.
  • Enums remove the need for public static final constants.

Defining an Enum

Defining an enum is as simple as creating any other class in Java.

  public enum Gender {
      MALE, FEMALE, UNKNOWN
  };

Read More

Tagged: design, Enumeration, Java

 

Disclaimer

The words and opinions expressed here are those of each article's respective author, and do not necessarily represent the views of CapTech Ventures.