Concepts / Java variable declarations

Java variable declarations

An enum defines a type with a fixed set of named values.

  • Programming

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?

  • Yes, because any name can be assigned to a variable

  • No, because Day.THURSDAY is not one of the fixed constants defined for Day

Reveal answer
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

Definition

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.

java
Notes on the code
  • 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.
Dayenum typeMONDAYenum constantTUESDAYenum constantWEDNESDAYenum constantcontainscontainscontains
What does an enum type contain? The enum type Day holds a fixed set of named constants, and each constant belongs to the type as a whole.

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.

java
Notes on the code
  • 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.
todayvariableDayenum typeDay.MONDAYenum constantdeclared as typecurrently holds
How does an enum-typed variable relate to the enum type, and which constant can it hold at a time? The variable today is declared as Day, and at any moment it holds one Day constant.

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.

Before

Day today;

today is declared with type Day but has not been assigned a constant yet.

After
Day today = Day.MONDAY;

today now holds the constant MONDAY from the Day enum.

Dayenum type name.separatorMONDAYconstant name
Which parts of Day.MONDAY are the enum type name and which are the constant name? The part before the dot names the type, and the part after the dot names the constant.
Worked example

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.

  1. 1
    Identify the enum type

    The type is Day, the enum name. It is the word that appears before the variable name in the declaration.

  2. 2
    Declare the variable

    Write Day meeting; This creates a variable whose type is Day. It does not yet hold a constant.

  3. 3
    Choose a constant from the enum

    TUESDAY is listed in the Day definition, so it is a valid constant for this variable.

  4. 4
    Write the full constant name

    Use the enum type name, a dot, and the constant name: Day.TUESDAY. The statement becomes Day meeting = Day.TUESDAY;

Result

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.

  1. 1
    Check the declaration: confirm that the variable was declared with the enum type, such as Day.
  2. 2
    Check the right-hand side: confirm that the value after the equals sign names a constant from that same enum.
  3. 3
    Check the constant name: confirm that the constant uses the full name, including the enum type name, such as Day.MONDAY.
  4. 4
    If 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 like
    Day today = MONDAY;
    Why it fails

    The 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 instead
    Day today = Day.MONDAY;
  • Common mistake

    Assigning a constant that belongs to a different enum

    What it looks like
    Day today = Season.SUMMER;
    Why it fails

    The 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 instead

    Use 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 like
    Day today = Day.THURSDAY;
    Why it fails

    An 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 instead

    Use 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

EASY

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.