Java Enum

Java Enum

Enum in java is a data type that contains fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The java enum constants are static and final implicitly. It is available from JDK 1.5.

Java Enums can be thought of as classes that have fixed set of constants.

Points to remember for Java Enum

  • enum improves type safety
  • enum can be easily used in switch
  • enum can be traversed
  • enum can have fields, constructors and methods
  • enum may implement many interfaces but cannot extend any class because it internally extends Enum class

Simple example of java enum

  1. class EnumExample1{
  2. public enum Season { WINTER, SPRING, SUMMER, FALL }
  3. public static void main(String[] args) {
  4. for (Season s : Season.values())
  5. System.out.println(s);
  6. }}

Test it Now

Output:WINTER
       SPRING
       SUMMER
       FALL
download this enum example

What is the purpose of values() method in enum?

The java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum.


Internal code generated by the compiler for the above example of enum type

The java compiler internally creates a static and final class that extends the Enum class as shown in the below example:

  1. public static final class EnumExample1$Season extends Enum
  2. {
  3.   private EnumExample1$Season(String s, int i)
  4.     {
  5.         super(s, i);
  6.     }
  7.     public static EnumExample1$Season[] values()
  8.     {
  9.         return (EnumExample1$Season[])$VALUES.clone();
  10.     }
  11.     public static EnumExample1$Season valueOf(String s)
  12.     {
  13.         return (EnumExample1$Season)Enum.valueOf(EnumExample1$Season, s);
  14.     }
  15.     public static final EnumExample1$Season WINTER;
  16.     public static final EnumExample1$Season SPRING;
  17.     public static final EnumExample1$Season SUMMER;
  18.     public static final EnumExample1$Season FALL;
  19.     private static final EnumExample1$Season $VALUES[];
  20.     static
  21.     {
  22.         WINTER = new EnumExample1$Season(“WINTER”0);
  23.         SPRING = new EnumExample1$Season(“SPRING”1);
  24.         SUMMER = new EnumExample1$Season(“SUMMER”2);
  25.         FALL = new EnumExample1$Season(“FALL”3);
  26.         $VALUES = (new EnumExample1$Season[] {
  27.             WINTER, SPRING, SUMMER, FALL
  28.         });
  29.     }
  30. }

Defining Java enum

The enum can be defined within or outside the class because it is similar to a class.

Java enum example: defined outside class

  1. enum Season { WINTER, SPRING, SUMMER, FALL }
  2. class EnumExample2{
  3. public static void main(String[] args) {
  4. Season s=Season.WINTER;
  5. System.out.println(s);
  6. }}

Test it Now

Output:WINTER

Java enum example: defined inside class

  1. class EnumExample3{
  2. enum Season { WINTER, SPRING, SUMMER, FALL; }//semicolon(;) is optional here
  3. public static void main(String[] args) {
  4. Season s=Season.WINTER;//enum type is required to access WINTER
  5. System.out.println(s);
  6. }}

Test it Now

Output:WINTER

Initializing specific values to the enum constants

The enum constants have initial value that starts from 0, 1, 2, 3 and so on. But we can initialize the specific value to the enum constants by defining fields and constructors. As specified earlier, Enum can have fields, constructors and methods.

Example of specifying initial value to the enum constants

  1. class EnumExample4{
  2. enum Season{
  3. WINTER(5), SPRING(10), SUMMER(15), FALL(20);
  4. private int value;
  5. private Season(int value){
  6. this.value=value;
  7. }
  8. }
  9. public static void main(String args[]){
  10. for (Season s : Season.values())
  11. System.out.println(s+” “+s.value);
  12. }}

Test it Now

download this enum example
Output:WINTER 5
       SPRING 10
       SUMMER 15
       FALL 20

Constructor of enum type is private. If you don’t declare private compiler internally creates private constructor.

  1. enum Season{
  2. WINTER(10),SUMMER(20);
  3. private int value;
  4. Season(int value){
  5. this.value=value;
  6. }
  7. }

Internal code generated by the compiler for the above example of enum type

  1. final class Season extends Enum
  2. {
  3.     public static Season[] values()
  4.     {
  5.         return (Season[])$VALUES.clone();
  6.     }
  7.     public static Season valueOf(String s)
  8.     {
  9.         return (Season)Enum.valueOf(Season, s);
  10.     }
  11.     private Season(String s, int i, int j)
  12.     {
  13.         super(s, i);
  14.         value = j;
  15.     }
  16.     public static final Season WINTER;
  17.     public static final Season SUMMER;
  18.     private int value;
  19.     private static final Season $VALUES[];
  20.     static
  21.     {
  22.         WINTER = new Season(“WINTER”010);
  23.         SUMMER = new Season(“SUMMER”120);
  24.         $VALUES = (new Season[] {
  25.             WINTER, SUMMER
  26.         });
  27.     }
  28. }

Can we create the instance of enum by new keyword?

No, because it contains private constructors only.

Can we have abstract method in enum?

Yes, ofcourse! we can have abstract methods and can provide the implementation of these methods.


Java enum in switch statement

We can apply enum on switch statement as in the given example:

Example of applying enum on switch statement

  1. class EnumExample5{
  2. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
  3. public static void main(String args[]){
  4. Day day=Day.MONDAY;
  5. switch(day){
  6. case SUNDAY:
  7.  System.out.println(“sunday”);
  8.  break;
  9. case MONDAY:
  10.  System.out.println(“monday”);
  11.  break;
  12. default:
  13. System.out.println(“other day”);
  14. }
  15. }}

Test it Now

download this enum example
Output:monday
Next TopicJava Annotation
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java Enum Enum in java is a data type that contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The java enum constants are static and final implicitly. It is available from JDK 1.5. Java […]

Tags:

....
...