Enumeration

In Java, an enumeration is a special type of class that represents a group of related constants. An enumeration defines a set of named values that are represented by unique identifiers. The values of an enumeration are fixed at compile time and cannot be changed at runtime.

Enumeration, often referred to as "enum" in programming, is a way to create a custom type that consists of a set of named constants. Here's a simple explanation:

  1. Creating Categories: Imagine you have different categories or groups of things that are related. In programming, an enum lets you group these similar items under one name. It's like creating a special list where each item has a specific name.

  2. Named Constants: Each item in an enum is a constant, which means its value does not change. These items are usually related. For example, you could have an enum named DaysOfWeek with items like Monday, Tuesday, Wednesday, and so on.

  3. Simplicity and Readability: Enums make your code simpler and more readable. Instead of remembering numerical codes or values for categories, you use meaningful names. For instance, using DaysOfWeek.Monday is more understandable than just remembering and using the number 1 for Monday.

  4. Fixed Values: The values in an enum are fixed. This means that an enum of days of the week can only contain those specific days. You can't accidentally insert a non-day or make a typo like Mondya, as the compiler won't recognize it.

  5. Use in Switch Statements: Enums are often used in switch statements in programming. Because each enum value is distinct, they work well in situations where you need to make decisions based on categories or specific values.

  6. Underlying Values: While enums are primarily used for their names, they often have underlying integer values associated with them. In many languages, these start from 0 and increase by 1 for each item, but you can usually set these values manually if needed.

Enums are a useful way to group and manage sets of related constants under a single umbrella. They make your code more understandable and less prone to errors caused by using incorrect or invalid values.

To create an enumeration in Java, you define a new class that extends the Enum class and defines a set of named constants as public static final fields. For example:

Enumeration is a data type in programming that allows you to define a set of named values, called enumerators or constants, that represent a fixed set of possible options or choices. Each enumerator has an associated integer value, starting from 0 and incrementing by 1 for each subsequent enumerator. This makes representing and manipulating sets of related options or choices in your program is easier.

public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }

In this example, the Day enumeration defines seven named constants for the days of the week.

You can use an enumeration in Java in a variety of ways. For example, you can use an enumeration as a parameter to a method or as the type of a variable. Here are some examples:

// using an enumeration as a parameter to a method public void doSomething(Day day) { switch (day) { case MONDAY: // do something on Monday break; case TUESDAY: // do something on Tuesday break; // ... and so on } } // using an enumeration as the type of a variable Day today = Day.MONDAY; System.out.println(today); // output: MONDAY

In addition to the named constants, an enumeration also provides some useful methods, such as values(), which returns an array of all the constants in the enumeration, and valueOf(String name), which returns the constant with the specified name.

Enumerations can be a powerful tool for creating clear and maintainable code that uses fixed sets of values. By using an enumeration, you can avoid using magic numbers and strings in your code, which can make it more readable and easier to understand.

Enumerated Methods

  • toString – returns name of calling constant•

  • ordinal – returns the zero-based position of the constant in the enum. For example the ordinal for Day.THURSDAY is 4•

  • equals – accepts an object as an argument and returns true if the argument is equal to the calling enum constant•

  • compareTo - accepts an object as an argument and returns a negative integer if the calling constant’s ordinal < than the argument’s ordinal, a positive integer if the calling constant’s ordinal > than the argument’s ordinal and zero if the calling constant’s ordinal == the argument’s ordinal.

/** * This program demonstrates an enumerated type. */ public class Demo6 { // Declare the Day enumerated type. enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public static void main(String[] args) { // Declare a Day variable and assign it a value. Day workDay = Day.WEDNESDAY; // The following statement displays WEDNESDAY. System.out.println(workDay); // The following statement displays the ordinal // value for Day.SUNDAY, which is 0. System.out.println("The ordinal value for " + Day.SUNDAY + " is " + Day.SUNDAY.ordinal()); // The following statement displays the ordinal // value for Day.SATURDAY, which is 6. System.out.println("The ordinal value for " + Day.SATURDAY + " is " + Day.SATURDAY.ordinal()); // The following statement compares two enum constants. if (Day.FRIDAY.compareTo(Day.MONDAY) > 0) System.out.println(Day.FRIDAY + " is greater than " + Day.MONDAY); else System.out.println(Day.FRIDAY + " is NOT greater than " + Day.MONDAY); } }

 

This Java program demonstrates the use of an enumerated type or "enum". An enum is a special "class" that represents a group of constants (unchangeable variables).

Here is a breakdown of the Java code:

  • First, an enum named Day is declared. This enum has seven constant values, one for each day of the week.

  • In the main method:

    • A Day variable named workDay is declared and assigned the value Day.WEDNESDAY.

    • The println statement prints out the value of workDay, which is WEDNESDAY.

    • Then two println statements are used to print the ordinal values of Day.SUNDAY and Day.SATURDAY. In an enum, each constant has an ordinal value, which is its position in the list of constants, starting from 0. Therefore, Day.SUNDAY has an ordinal value of 0 and Day.SATURDAY has an ordinal value of 6.

    • Lastly, the compareTo method is used to compare the ordinal values of two Day constants, Day.FRIDAY and Day.MONDAY. This method returns a negative value if the first constant appears before the second, zero if they are the same, and a positive value if the first constant appears after the second. So, if Day.FRIDAY comes after Day.MONDAY in the Day enum, the statement "FRIDAY is greater than MONDAY" will be printed to the console. If not, the statement "FRIDAY is NOT greater than MONDAY" will be printed.

So, this program is a basic demonstration of declaring and using enums in Java. It shows how to declare an enum, assign an enum value to a variable, print the value of an enum constant, get the ordinal value of an enum constant, and compare two enum constants.

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark