Instructional Material: Enums

Lots of times when programming we need to assign constant values to track the various states of a data item. For example, in a program we have an integer variable that indicates the day of the week. We can define a convention where the integer value zero is assigned to mean Sunday, the value of 1 to mean Monday, 2 to mean Tuesday and so on. When coding our program we have to remember that 2 means Tuesday. This tracking of numeric values and what they mean for various variables can get cumbersome and error prone in more complex programs. Another way to track the values associated with various states of a variable is with enums.

An enum or enumeration, is a special data type (an object) defined by Java. It is a list of constant numeric values known at compile time by names. When a variable of the enum type is created it is assigned one of it's pre-defined constant value names and can only have one of it's constant names as its value at a time. Because they represent constants, the names assigned are by convention in upper case. Here is an example:

Internally, Sunday is assigned a value of zero by the compiler, Monday a value of 1 and so on. The key concept is using names instead of constant values, so instead of having to remember what 1 or 6 or whatever some number means in the context of some variable, you use a name. Using names improves readability and reliability of code by making things more obvious. So how do we use enums? Here are some examples:

Here we created a variable day of enum type DayOfWeek and set it to TUESDAY. We can then use day in various ways. enums are best used to represent a fixed set of constants, like day of week, planets of the solar system, menu choices, any list of constants whose values are all known at compile time. Internally by default, each enum name is assigned a constant integer value starting at zero, but in the simple enum case, we don't care as we want to use the names not the numbers. Data types other than integers may be used (see below).
 
To make things more interesting, remember that an enum is a special form of a class. Therefore it can have fields and methods. By default, enum variables don't tell you their underlying constant value, only the name the variable is set to. We can extend the enum in the above example to track and return the underlying constant value of the enum to demonstrate extending the enum with fields and methods:

Here we added an int field to the enum and defined a constructor to set the value of the field. We also added a method to get the day number and made the field public so we can access it directly. Note that when using a constructor, we are required to explicitly define the constants for each name. Note in this example the numeric values assigned to the enum names are not in sequence. They don't have to be.
 
You can assign any primitive or String constant value you wish to the names and you can have more that one data value for each name. However, when you want to use a constant value other than integer or have more that one constant value associated with a name, you must use a constructor to define the data types. See the detailed discussion for more about this.
 
Enums are very useful in their basic form and given that they have the full capability of classes, they can be extended to be very powerful as in the Planets example in the detailed discussion.
 
Here is a video about enums. Here is a detailed discussion of enums.
 
Here is the example code in CodingGround for you to experiment with. Complete both enums for a full 7 days and test. After the next lesson on Arrays, return here and modify the example to print out a list of all of the values in the enums. You will need to view the video above to see how.
 
Material Type: 
Lecture/Presentation
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
EV3
RoboRIO
Tetrix
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner