Java variable declarations
An enum defines a type with a fixed set of named values.
Why Some Variables Need a Fixed List
Some variables should represent one choice from a known list rather than an unrestricted value. A day of the week, a traffic light color, or a shipping status are examples: the program should only ever hold one of a small, known set of options. Java handles this kind of variable with an enum, which is a type whose possible values are fixed when the type is created.
What do you think happens?
Suppose an enum named Day is defined with the constants MONDAY, TUESDAY, and WEDNESDAY. Is Day.THURSDAY a valid constant that a Day variable can hold?
Reveal answer
No, because Day.THURSDAY is not one of the fixed constants defined for Day
An enum's possible values are fixed when the type is defined. A Day variable can only hold one of the constants listed in the Day definition, so a value that was never listed is not allowed.
Defining an Enum Type
An enum defines a type with a fixed set of named values. The named values are called enum constants, and they are listed inside the enum definition.
- The keyword enum introduces a new type named Day.
- MONDAY, TUESDAY, and WEDNESDAY are the enum constants. They are the only values this type has.
The diagram shows the relationship: the enum type is the container, and its named values are the only members it can ever have. When you read the definition, the list inside the braces is the complete set of allowed values for that type.
Declaring an Enum-Typed Variable
Once an enum is defined, its name can be used as a variable type. A declaration has the same shape as any other Java variable declaration: the type comes first, then the variable name. Here the type is Day, so the variable can hold a Day constant.
- Day is the type, taken from the enum name.
- today is the variable name. It is declared but has not been given a constant yet.
Assigning a Constant by Its Full Name
After declaring the variable, you give it one of the enum's constants. Enum constants are written with the enum type name and a dot before the constant. The full name therefore has two parts that must both be correct: the type name and the constant name.
Day today;
today is declared with type Day but has not been assigned a constant yet.
Day today = Day.MONDAY;today now holds the constant MONDAY from the Day enum.
Assigning a constant to a Day variable
Given the enum Day with constants MONDAY, TUESDAY, and WEDNESDAY, declare a variable named meeting that can hold a Day, then assign it the constant TUESDAY.
- 1Identify the enum type
The type is Day, the enum name. It is the word that appears before the variable name in the declaration.
- 2Declare the variable
Write Day meeting; This creates a variable whose type is Day. It does not yet hold a constant.
- 3Choose a constant from the enum
TUESDAY is listed in the Day definition, so it is a valid constant for this variable.
- 4Write the full constant name
Use the enum type name, a dot, and the constant name: Day.TUESDAY. The statement becomes Day meeting = Day.TUESDAY;
The variable meeting is of type Day and holds the constant Day.TUESDAY.
Checking an Enum Assignment Step by Step
When an enum assignment does not match what you expected, inspect the statement from left to right. The check is a dependable way to find the cause, because each part of the statement has one specific requirement.
- 1Check the declaration: confirm that the variable was declared with the enum type, such as Day.
- 2Check the right-hand side: confirm that the value after the equals sign names a constant from that same enum.
- 3Check the constant name: confirm that the constant uses the full name, including the enum type name, such as Day.MONDAY.
- 4If all three checks pass, compare the constant name itself with the enum definition to confirm it is spelled exactly as listed.
Mistakes Beginners Make with Enum Assignments
Common mistake Leaving out the enum type name in the constant
What it looks likeDay today = MONDAY;Why it failsThe constant belongs to the Day type, so its full name includes Day and the dot. The bare name MONDAY is not used this way in this context.
Do this insteadDay today = Day.MONDAY;Common mistake Assigning a constant that belongs to a different enum
What it looks likeDay today = Season.SUMMER;Why it failsThe variable was declared with type Day, but the right-hand side names a constant from Season. The constant must come from the same enum as the variable's type.
Do this insteadUse a constant listed in the Day definition, such as Day.MONDAY, or change the variable's declared type to Season.
Common mistake Using a constant that was never listed in the enum
What it looks likeDay today = Day.THURSDAY;Why it failsAn enum's possible values are fixed when the type is defined. If THURSDAY is not in the Day definition, it is not a valid constant.
Do this insteadUse one of the listed constants, or add the constant to the enum definition if the design needs it.
Common mistake Declaring the variable with the wrong type name, so the variable is not of the enum type the constant comes from
Practice: Enum Assignment
Given the enum definition enum Color { RED, GREEN, BLUE }, write a declaration of a variable named paint whose type is Color, then assign it the constant GREEN using the full name. After writing it, use the three-step check from this article to confirm that the type name and the constant name both match the enum definition.
Hints
- The declaration starts with the enum type name Color, followed by the variable name.
- The constant must be written as Color.GREEN, with the enum type name and a dot.
- Check that GREEN appears in the Color definition and that the variable was declared with type Color.
Key Takeaways
- An enum defines a type with a fixed set of named values, and those values are fixed when the type is created.
- The enum name can be used as a variable type, so a variable of that type holds one constant from the enum at a time.
- Enum constants are written with the enum type name and a dot before the constant, as in Day.MONDAY.
- When an assignment fails, check the declaration, then the right-hand side, then the full constant name against the enum definition.