Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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.

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.
 * Book Example
 */

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.

  • No labels