CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Packages

Packages
Enumerations
Declare

Java allows programmers to create their own data types by specifying the values of that data type.These are called enumeration or enum types and are defined using the keyword enum. The values that you specify for the data types are identifiers.

How to declare and work with enumerations

  • An enumeration contains a set of related constants.
  • The constants are defined with the int type and are assigned values from 0 to the number of constants in the enumeration minus 1.
  • An enumeration defines a type. Because of that, you can't specify another type where an enumeration type is expected. That means that enumerations are type-safe.
  • One restriction when declaring an enumerated type is that it cannot be a local declaration. In other words, we must declare it outside of any method, just as for the other data members of a class.

The syntax for declaring an enumeration

public enum EnumerationName
{
    CONSTANT_NAME1[,CONSTANT_NAME2]...
}
Previous | Declare | Use | Enhance | Work with static imports | Application