Enum
layout: default
title: enum Type
enum is a data type (like class and interface) that allows a variable to be a set of predefined constants.
- An
enumcan be in its own.javafile, or part of another class.
Use enum types when you need to represent a fixed set of constants, such as:
* the names of planets.
* a set of command-line flags.
* menu options.
Conventions¶
The name of the type is singular, like creating a class, because callers will use one of the discrete constant values.
public enum Color {
// ...
}
The members of the enum are constants, so we capitalize and use snake-case.
public enum Color {
RED, GREEN, BLUE // comma-separated, no need for a ;
}
Drill¶
EnumeratedTypes/com.example.enums.drills
- Create a new
enumnamedDayfor the days of the week, starting with Sunday.- Be sure to observe naming conventions.