Monday, 25 April 2016

Key points about Enums


KEY POINTS ABOUT ENUMS


1. Type-safe
We cannot assign anything else other than predefined enum constants to an enum variable. otherwise a compiler error will occur.

public enum WorkDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY};
WorkDay day1 = WorkDay.MONDAY;
day1 = "Mon"; // compilation error

2. You can define constructor, methods and variables inside java Enum, as Enum is reference type like class or interface.
Enum can implement the interface and override any method (Like, toString) like a normal class.

3. Enum have its own name-space
    We can use same enum constant field name with another enum declaration.

4. Enum constants are implicitly static & final and cannot be changed once created.
  WorkDay.MONDAY = WorkDay.TUESDAY;

5. Enum can be used inside switch statement

6. Values of enum constants can be specified at the creation time.
    (For this, we need to define a member variable and a private constructor)
  public enum WorkDay {
    MONDAY(1)TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5);
    private int num;

    private WorkDay(int num) {
       this.num = num;
    }
  };

7. Constructor of enum must be private
   Private constructor of Enum is not allowed to create instance of enums by using new keyword.
     Enums constants can only be created inside Enums itself.

8. Methods can also be defined inside Enum
  public enum WorkDay {
    MONDAY(1)TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5);
    private int num;

    private WorkDay(int num) {
       this.num = num;
    }
     
    public String getValue() {
       return "Value: " + num;
    }   
  };


9. You can safely compare Enum constants using "==" equality operator.
  Currency day1 = WorkDay.MONDAY;
  if(day1 == WorkDay.TUESDAY){ ... }

10. Java compiler automatically generates static values() method for every enum which returns array of Enum constants in the same order they have listed in enum.

12. Two new collection classes EnumMap and EnumSet are added into collection package to support Java Enum.
These classes are high performance implementation of Map and Set interface.

13. Instance of Enum is created when any Enum constants are first called or referenced in code (Loading of enum in JVM)

14. Enum are implicitly implement both Serializable and Comparable interface.

15. We can define abstract methods inside Enum and provide different implementation for different instances of enum.

  public enum WorkDay {
    MONDAY(1) {
      public String shortCode() {
          return "MON";
      }
    }, 
    TUESDAY(2) {
      public String shortCode() {
          return "TUE";
      }
    }, 
    ....


    private int num;

    private WorkDay(int num) {
       this.num = num;
    }
    
    /** ABSTRACT METHOD **/ 
    public abstract String shortCode();

    public String toString() {
       return this.name() + this.num;
    }   
  };

No comments:

Post a Comment

Note: only a member of this blog may post a comment.