Broad Network


Java Enum Type Basics

Java Just After the Basics – Part 6

Forward: In this part of the series, I explain the basics of what is known as enum type. This is a kind of class in Java.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 6 of my series, Java Just After the Basics. In this part of the series, I explain the basics of what is known as enum type. This is a kind of class in Java.

Illustration
What are the four compass directions? They are: NORTH, SOUTH, EAST, WEST. These are constants (cannot change) so Java expects them in uppercase letters.

What are the seven days of the week? They are: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY. These are constants (cannot change) so Java expects them in uppercase letters.

What are the eight planets (formerly nine) of the solar system? They are: MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE. These are constants (cannot change) so Java expects them in uppercase letters. It used to be thought that there are nine planets in the solar system, but scientists have proven that the apparent ninth planet, Pluto, is not really a planet. It seems it is two bodies rotating about one another; it does not have a main body like the other planets. For this tutorial, consider that there are eight planets.

Known Fixed Set of Constants
I have given you three sets above: the set of the compass directions; the set of days of the week; and the set of planets in the solar system. A class can be considered to have a set of properties (variables), however the number of properties is not fixed, that is, any class can have any number of properties; it is possible to even increase the number of properties of a class (see later). In the above cases, we are dealing with a fixed number of constants (variables) in a known complete set. When dealing with a fixed set of constants, you use the enum type and not an ordinary class type.

The enum Type
In simple terms, the syntax to create an enum type is:

    enum EnumName {//list of constants}

enum is a reserved word, which means enum type (data type); it is in lowercase letters. The enum type is a kind of class.

For the compass directions you would have:

    enum CompassDirection {NORTH, SOUTH, EAST, WEST}

For the days of the week you would have:

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

For the planets, you would have:

    SolarPlanet {MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE}

For the months of the year you would have:

    enum Month {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER}

The items of the list in an emun type are separated by commas. There is no comma after the last item.

Note, the constants are represented by the compiler with different integers; you do not have to know the integers. You can have spaces between the list items (the spaces are optional).

Non-Common Fixed Set of Constants
In programming, there are some fixed sets of constants that are not common. An example is the choices of a menu. Another example is the choices of a restaurant menu. The following enum type shows simple choices of a very simple restaurant.

    enum Food {BEANS, RICE, POTATO}

From these choices, a customer can eat beans, or rice or potato (not any combination).

Using the enum Type
You can use an enum type in a number of ways; one use is in the switch construct as in the program below. The enum type is a class and the constants are like static class properties. The enum type has a special way of instantiation, which is to declare an object of the enum type and then assign a property constant to it. The syntax is:

    EnumName enumObjectName = EnumName.Constant;

In the case of the restaurant menu, when the customer requests a meal (say by clicking some button), the output of the program below will say what the customer should be served with. In this case and if the customer chooses rice, the instantiation would be:

    Food food = Food.RICE;

Here, the object from the enum class, “Food” is, “food”; the constant chosen is RICE of the static expression, “Food.RICE”. The statement can be separated into declaration and (first) assignment. In the switch construct, you just use the constants without the preceding enum class name, Food, because the conditional expression in the parentheses of the switch construct takes, food. Read and try the following code:

enum Food {BEANS, RICE, POTATO}

class EnumProg
    {
        public static void main(String[] args)
            {
                Food food = Food.RICE; //choice of customer is rice

                switch (food) {
                                 case BEANS:
                                     System.out.println("Customer will be served beans.");
                                     break;
                    
                                 case RICE:
                                     System.out.println("Customer will be served rice.");
                                     break;
                        
                                 case POTATO:
                                     System.out.println("Customer will be served potato.");
                                     break;
                        
                                 default:
                                     System.out.println("Customer cannot be served here.");
                                     break;
                             }
            }
    }

Note: the instantiation of an enum object involves the choice made (item chosen). The enum class is a list of constants from which a choice has to be made.

Integer Nature of Constants
The conditional expression, such as “food”, in the parentheses of the switch construct has to boil down to an integer. So, the enumeration constants boil down to integers. You do not need to know which integer a constant results in. The instantiated object name of the enum type represents (is a variable for) any of the integers of the constants. In the switch construct, “case” actually means, “if the integer is”.

More than one-word Constant
If you want a constant of more than one word, use the underscore to separate the words.

That is it for this part of the series. We stop here and continue in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message