Friday, 9 October 2015

What is Enum Types?

Enum Types - jdk1.5

An enum type is a special data type that enables for a variable to be a set of predefined constants. enum constants are implicitly static and final and we cannot change their value once created.

Example: Number of days in Week, Number of planets in Solar system and compass directions (values of NORTH, SOUTH, EAST, and WEST) etc.

Constant without enum (prior JDK 1.5)

public static final constant cab be used to replicate enum like behaviour.

public class CurrencyDenom {
      public static final int PENNY = 1;
      public static final int NICKLE = 5;
      public static final int DIME = 10;
      public static final int QUARTER = 25;

public class Currency {
      private int currency;
      // CurrencyDenom.DIME,CurrencyDenom.QUARTER

Some serious limitations:

No Type-Safety: First of all it’s not type-safe; you can assign any valid int value to currency e.g. 99 though there is no coin to represent that value.

public class Currency {
      private int currency = CurrencyDenom.PENNY;
      currency = 99; // invalid currency

No Meaningful Printing: printing value of any of these constant will print its numeric value instead of meaningful name of coin e.g. when you print NICKLE it will print "5" instead of "NICKLE".

No namespace: to access the currencyDenom constant we need to prefix class name e.g. CurrencyDenom.PENNY.
There may be ambiguity when PENNY is static imported from two classes.

Benefits of Java enum: solution of all this limitation

public enum Currency {PENNY, NICKLE, DIME, QUARTER};

Here Currency is our enum and PENNY, NICKLE, DIME, QUARTER are enum constants.

Notice curly braces around enum constants because Enum are type like class and interface in Java.

1. enum is Type-safe, we cannot assign anything else other than predefined enum constants to an enum variable. It is compiler error to assign something else unlike the public static final variable.

2. enum has its own name-space, We can use same enum constant field name with another enum declaration.

3. enum can be used inside switch statement like int or char primitive data type.

enum Day {

public class EnumTest {
      Day day;

      public EnumTest(Day day) {
   = day;

      public void tellItLikeItIs() {
            switch (day) {
            case MONDAY:
                  System.out.println("Mondays are bad.");

            case FRIDAY:
                  System.out.println("Fridays are better.");

            case SATURDAY:
            case SUNDAY:
                  System.out.println("Weekends are best.");

                  System.out.println("Midweek days are so-so.");

      public static void main(String[] args) {
            EnumTest firstDay = new EnumTest(Day.MONDAY);
            EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
            EnumTest fifthDay = new EnumTest(Day.FRIDAY);
            EnumTest sixthDay = new EnumTest(Day.SATURDAY);
            EnumTest seventhDay = new EnumTest(Day.SUNDAY);
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...