Home   Cover Cover Cover Cover
 

Enumerations

A variable

  Color c;

may only be assigned values of type Color (e.g. Color.red, Color.blue und Color.green). In contrast, a variable

  int c;

may be assigned not only integer constants red, blue and green but also any other int values. The compiler can therefore perform stricter type checks with enumerations. For example, it can make sure that only enumeration constants are assigned to a variable of an enumeration type.

Another benefit of enumeration constants is that they have to be qualified with the name of the corresponding enumeration type, making it explicit to which type those constants belong. Furthermore, there cannot be a name clash between enumeration constants of different enumeration types.