enums in Java

We can use enum to define a group of named constants. By using enum we can define our own data types. enum concept introduced in 1.5v. Java's enum are more powerful than enum in other languages.

Declaration enum

  • enum concept is internally implemented using class concept. Syntax of enum declaration are almost same as class declaration. enum keyword is used for declaring enums.
//EXAMPLE CODE-1
public enum Month{
    JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
}
In above example, We have declared Month as enum and JAN, FEB, MAR ...DEC are constants of type Month.
A enum can have instance variables, method and constructor. Instance variables, method and constructor must be declared after enum constants declaration. If there are parameterised constructors in enum then enum constant must have these parameters.
//EXAMPLE CODE-2
public enum Day {
 SUN("Sunday"), MON("Monday"), TUE("Tuesday"), WED("Wednesday"),
THU("Thursday"),FRI("Friday"),SAT("Saturday");
 private String value;
 private Day(String value){
  this.value = value;
 }
 public String getValue() {
  return value;
 }
}

Day enum declared in above program have constructor, instance variable -value and method- getValue(). enum constant has parameter as String.
  • Every enum constant is a reference variable to enum type object. In above example, SUN, MON etc. are instances of  Day enum.
  • Every enum constant is always public static final by default.
  • Constructor can be declared as private and default but can't be public and protected
  • A enum can be declared within a class or outside of class but not in method..
  • If we declare a enum outside of class the applicable modifiers are  - public, default and strictfp
  • If we declare a enum within a class the applicable modifiers are  - public, default, strictfp, privat,protected and static

Internals of enum

Let’s see how enum internally work to simplify above explanation. Run javap  -p Month.class command on compiled code of above EXAMPLE CODE-1.  This will give you below code as output of command - 
Compiled from "Month.java"
public final class Month extends java.lang.Enum {
  public static final Month JAN;
  public static final Month FEB;
  public static final Month MAR;
  public static final Month APR;
  public static final Month MAY;
  public static final Month JUN;
  public static final Month JUL;
  public static final Month AUG;
  public static final Month SEP;
  public static final Month OCT;
  public static final Month NOV;
  public static final Month DEC;
  private static final Month[] ENUM$VALUES;
  static {};
  private Month(java.lang.String, int);
  public static Month[] values();
  public static Month valueOf(java.lang.String);
}

You can read more about javap command on java doc.  Please note that above code does not has body for constructor, static block and method. So let’s re-write above code with body of these blocks. A class can not extends java.lang.Enum class explicitly, below code is just for understanding internal of enum. 

 
public final class Month extends java.lang.Enum {
   public static final Month JAN;
   public static final Month FEB;
   public static final Month MAR;
   public static final Month APR;
   public static final Month MAY;
   public static final Month JUN;
   public static final Month JUL;
   public static final Month AUG;
   public static final Month SEP;
   public static final Month OCT;
   public static final Month NOV;
   public static final Month DEC;
   private static final Month[] ENUM$VALUES;
   static {
    JAN = new Month("JAN", 0);
    FEB = new Month("FEB", 1);
    MAR = new Month("MAR", 2);
    APR = new Month("APR", 3);
    MAY = new Month("MAY", 4);
    JUN = new Month("JUN", 5);
    JUL = new Month("JUL", 6);
    AUG = new Month("AUG", 7);
    SEP = new Month("SEP", 8);
    OCT = new Month("OCT", 9);
    NOV = new Month("NOV", 10);
    DEC = new Month("DEC", 11);
    ENUM$VALUES = new Month[]{JAN,FEB, MAR, APR, MAY, 
      JUN, JUL, AUG, SEP, OCT, NOV, DEC};
   };
   private Month(java.lang.String name, int ordinal){
    super(name, ordinal);
   }
   public static Month[] values(){
    return ENUM$VALUES;
   }
}


Inheritance in enum

  1. every enum in java is direct child class of java.lang.Enum class. There is no chance of extending any other class because java does not support multiple inheritance.
  2. enum is always final implicitly so one enum can not extend another enum.
  3. enum can implement any number of interfaces.

Comments

Post a Comment

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java