Home > DeveloperSection > Articles > Java Enumerations: Adding Properties to an Enumeration

Java Enumerations: Adding Properties to an Enumeration


Java Java 
Ratings:
0 Comment(s)
 152  View(s)
Rate this:

Java Enumerations: Adding Properties to an Enumeration


To add a property to an enumeration constant, we would need to define its constructor. The enum class has only a single protected constructor that cannot be invoked by a programmer.

To illustrate, let’s create an enumeration of apples. Apples come in several varieties. Each variety has a specific name and a price that may be different from the other varieties.

A constructor for Apple will facilitate setting its price at the time of its construction. We can also define a method to retrieve the price of each apple.

Program Code                 

This program is shown here

enum Apple {

                     AURORA(10), BELMAC(12), CORTLAND(15), EMPIRE(8), GRAVENSTEIN(11);

                     private int price;

 

                     // Constructor

                     Apple(int price) {

                             this.price = price;

                     }

 

                     int getPrice() {

                             return price;

                     }

}

 

public class ApplesEnum {

                     public static void main(String args[]) {

                             System.out.println("Apple price list:");

                             for (Apple apple : Apple.values()) {

                                     System.out.println(apple + " costs " + apple.getPrice() + " cents.");

                             }

                     }

}

 

Explanation

The Apple enumeration declares five different varieties:

·         AURORA(10)

·         BELMAC(12)

·         CORTLAND(15)

·         EMPIRE(8)

·         GRAVENSTEIN(11)

Each constant declaration now accepts a parameter. In fact, each time a constant is declared, its constructor is called, and this parameter is sent as an argument to the constructor. The constructor is declared the same way as for any other class declaration:

Apple(int price) {

         this.price = price;

}

 

 

The parameter price is copied to a private instance variable. Thus, when we declare the constant AURORA, its price will be set to its input parameter value, which is 10. Likewise, BELMAC gets a price of 12, and so on. We also define a getPrice method that returns this price to the caller.

In the main function, we print the name and price of each apple using a foreach loop:

for (Apple apple : Apple.values()) {

                  System.out.println(apple + " costs " + apple.getPrice() + " cents.");

}

 

 

Note that the call to apple in this print statement calls its default toString method. The explicit call to getPrice returns the price of the specified instance.

We could easily add more apple varieties to this enumeration and/or change the order of these varieties without breaking the client code. This solves the problem with the integer pattern for enumerations discussed earlier.

The ordinal and compareTo Methods

The two most useful methods of the enum class are

·         ordinal

·         compareTo.

The ordinal method returns the ordinal value of the current enumeration constant, which is its position in the list of constants. The first constant in the list takes an ordinal value of 0. Thus, MONDAY in the DaysOfTheWeek enumeration has an ordinal value of 0, TUESDAY has a value of 1, and so on.

The compareTo method compares this enumeration with another object for its order in the enumeration. The method returns:

·         a negative integer

·         zero or

·         positive integer

Depending on whether the current object appears before, is equal to, or appears after the specified object.

For example, if the current day instance is DaysOfTheWeek.WEDNESDAY and we compare it with DaysOfTheWeek.SATURDAY, the function returns a value of –3. If we compare it with DaysOfTheWeek.MONDAY, the return value is +2.


Don't want to miss updates? Please click the below button!

Follow MindStick