Working with Java Enumerated types (Enums)

In this post I would like to explain about Enums in Java. Though in my 2 years of coding in Java I have seldom used Enums but they do provide a lot of features when we are required to create a say limited instances of certain type. In this post I have made use of an example written out of inspiration from the example program given in “A Programmer’s Guide to Java SCJP certification” by Khalid A. Mughal and Rolf W. Rasmussen. The example is given in the Section Enumerated Types. I will be developing the Enum as we go through the article.

What is an Enumerated Type or Enum tpye? Enum Type defines a finite set of symbolic names and their values. Suppose we have to define 3 constants- LOW, MEDIUM and HIGH to denote the 3 speeds of Rotation. We can do it by declaring static final variables as follows-

class Speed{
   public static final int LOW=1;
   public static final int MEDIUM=2;
   public static final int HIGH=3;
}

Though the above code serves the purpose there are few problems with it. Few of them are-
>> It declares the variables as plain int’s which means that I can pass any int value say 20, 1000 … whenever we are to pass a valid speed. So one can imagine the impact when the programmer is free to pass any valid int for the Speed values.
>> Also one can note that there’s no value to these “LOW”, “MEDIUM”, “HIGH”. They are just another set of variables. We cannot use them as types or different states of the machine.

To overcome these and other problems Enumerated types were introduced. How can we rewrite the above using Enums?

enum Speed{LOW,MEDIUM,HIGH}

This is the canonical form of declaring the enums. The keyword “enum” is used to declare Enums. The enum declares a comma separated list of enum constants. These enum constants are static members and are implicitly initialized when the enum is loaded at runtime. As they are static these enum constants can be accessed by using the Enum type name. Lets see how the Speed enum can be used-

enum Speed{LOW,MEDIUM,HIGH}
public class Machine{
   Speed machineSpeed;
   Machine(Speed speed){
     this.machineSpeed=speed;
   }
   Speed getSpeed(){
     return this.machineSpeed;
   }
   public static void main(String[] args){
     Machine machine1 = new Machine(Speed.LOW);
     Machine machine2 = new Machine(Speed.HIGH);
     System.out.println("Machine 1 speed: "+machine1.getSpeed());
     System.out.println("Machine 2 speed: "+machine2.getSpeed());
   }
}

Enums can have their own methods and constructors. Lets see how it can be done with an example-
Lets edit the Speed enum to take the speed values of our choice. By default LOW is of ordinal value 0, MEDIUM is of 1 and HIGH is of 2. Also we want to store differnt unit consumption for different speeds.

enum Speed{
   LOW(3,2.5),MEDIUM(6,5.0),HIGH(10,7.5);    //Line 1
   private int speedValue;
   private double unitsConsumed;
   Speed(int speed, double units){
     this.speedValue=speed;
     this.unitsConsumed=units;
   }
   public int getSpeedValue(){
     return this.speedValue;
   }
   public double getUnitsConsumed(){
     return this.unitsConsumed;
   }
}
public class Machine{
   Speed machineSpeed;
   Machine(Speed speed){
    this.machineSpeed=speed;
   }
   Speed getSpeed(){
     return this.machineSpeed;
   }
   public static void main(String[] args){
     Machine machine1 = new Machine(Speed.LOW);
     Machine machine2 = new Machine(Speed.HIGH);
     System.out.println("Machine 1 speed: "+machine1.getSpeed().getSpeedValue()+" Units Consumed: "+machine1.getSpeed().getUnitsConsumed());
     System.out.println("Machine 2 speed: "+machine2.getSpeed().getSpeedValue()+" Units Consumed: "+machine2.getSpeed().getUnitsConsumed());
   }
}

The only way to create instances of Enum types is by specifying the Enum constant within the Enum type. One cannot use the “new” to create instances of the Enum type. In the above example Line 1 creates three instances of the Speed enum and it used the overloaded constructor for creating the instances. Also not the private variables and the public methods defined within the Enum type. Suppose I want to override few methods defined in the enum type according to the enum constant type, how do we do that? Let me take the same example and explain how to override the methods. Lets say i want to get specific information regarding each of the different speeds. So i add a method getInformation().

enum Speed{
   LOW(3,2.5){ //Line 1
     String getInformation(){ //Method overriden by the Enum constants
       return "Running of Low speed with less units consumed";
     }
   },
   MEDIUM(6,5.0){ //Line 2
     String getInformation(){
       return "Running of Medium speed with moderate units consumed";
     }
   },
   HIGH(10,7.5){ //Line 3
     String getInformation(){
       return "Running of High speed with Maximum units consumed";
     }
   };
   private int speedValue;
   private double unitsConsumed;
   Speed(int speed, double units){
     this.speedValue=speed;
     this.unitsConsumed=units;
   }
   public int getSpeedValue(){
     return this.speedValue;
   }
   public double getUnitsConsumed(){
     return this.unitsConsumed;
   }
   abstract String getInformation(); //This is an abstract method
}
public class Machine{
   Speed machineSpeed;
   Machine(Speed speed){
     this.machineSpeed=speed;
   }
   Speed getSpeed(){
     return this.machineSpeed;
   }
   public static void main(String[] args){
     Machine machine1 = new Machine(Speed.LOW);
     Machine machine2 = new Machine(Speed.HIGH);
     System.out.println("Machine 1 speed: "+machine1.getSpeed().getInformation());
     System.out.println("Machine 2 speed: "+machine2.getSpeed().getInformation());
   }
}

One can see in the above code this lines commented as Line 1, Line 2, Line 3 are overriding the abstract method getInformation(). They are actually implicitly extending the enum type. These are similar to the anonymous Inner classes.

Will update this post to add few more Enum related facts.

14 thoughts on “Working with Java Enumerated types (Enums)”

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading