Things Which You Don’t Know about Java Enum

What You Don’t Know about Java Enum

What You Don’t Know about Java Enum

Today am going to discuss about the things which I know about java Enum. Enum specifies a list of constant value of a specified type. Many people imagined that Enum is used to maintain a List of String Constants, but that’s wrong, Enum constant is of type Enum.

Ex:

RED, GREEN, YELLOW is of enum Type SIGNAL

We have told that enum is a list of constant value of a specified enum Type. So what does that mean. Lets see that with some example.

package com.learnfromexamples.enumtutorial.pkg2;

public enum Signal {
    RED,GREEN,YELLOW
}

Imagine the above mentioned enum as a below mentioned class

public class Signal{
    public static final Signal RED = new Signal("RED",0);
    public static final Signal GREEN = new Signal("GREEN",1);
    public static final Signal YELLOW = new Signal("YELLOW",2);
}

In java we will mention constant as static and final, similarly Enum can be represented like that.

Enum can be declared either as a separate class or as a class member but not inside a method. We will see with some examples now.

  • Enum as a class member

Signal.java


 package com.learnfromexamples.enumtutorial.pkg2;

 public enum Signal {
     RED,GREEN,YELLOW
 }

 
 package com.learnfromexamples.enumtutorial.pkg2;

 public class SignalImplementation {
     //Enum can be declared as a class member or as separate class
     //But Enum cant be declared inside a method
         
     public static void main(String[] args) {
         System.out.println(Signal.GREEN); // not printing String, its printing Signal instance
         System.out.println(Signal.RED);
         System.out.println(Signal.YELLOW);
         System.out.println(Signal.GREEN instanceof Signal);
         System.out.println(Signal.RED instanceof Signal);
         System.out.println(Signal.YELLOW instanceof Signal);
     }
 }

 

 

Output

 GREEN
 RED
 YELLOW
 true
 true
 true
 
  • Enum can be declared as a own class too.


SignalImplementation.java

 package com.learnfromexamples.enumtutorial.pkg2;

 public class SignalImplementation {
     //Enum can be declared as a class member or as separate class
     //But Enum cant be declared inside a method
     
     //Example One
     enum Season{
         SPRING, SUMMER, AUTUMN, WINTER
     }
     
     public static void main(String[] args) {
         System.out.println(Season.SPRING);
         System.out.println(Season.SUMMER);
         System.out.println(Season.WINTER);
         System.out.println(Season.AUTUMN);    
     }
 }

 

Output

 SPRING
 SUMMER
 WINTER
 AUTUMN
 

If we see both the outputs, it’s not printing the String; instead it’s printing the Enum type.

  • Enum will have constructors, methods , instance variables. Enum constructors can never be invoked directly. Instead whenever enum is initialized, Enum constructor will be called.

Rating.java

package com.learnfromexamples.enumtutorial.pkg1;

public enum Rating {
    EA(10), EM(20),MA(30),MN(40);
    int rate;
    Rating(int rate){
        this.rate = rate;
    }
    public int getRate() {
        return rate;
    }
}

RatingImplementation.java

package com.learnfromexamples.enumtutorial.pkg1;

public class RatingImplementation {
    public static void main(String[] args) {
       
        Rating rating = Rating.EA;
        System.out.println(rating);
        System.out.println(rating.getRate());
    }
}

Output

EA
10
  • Each Enum can have methods, In the above example we have a method called getRate(), now if we want to know rating class we can include methods as below.

Rating.java

package com.learnfromexamples.enumtutorial.pkg1;

public enum Rating {
    EA(10){
        public String getRatingClass() {
            return "Above Average";
        }
    
    },
    EM(20),
    MA(30),
    MN(40){
        public String getRatingClass() {
            return "Below Average";
        }
    };
    int rate;
    Rating(int rate){
        this.rate = rate;
    }
    public int getRate() {
        return rate;
    }
    
    public String getRatingClass() {
        return "Average";
    }
}

RatingImplementation.java

package com.learnfromexamples.enumtutorial.pkg1;

public class RatingImplementation {
    public static void main(String[] args) {
        
        Rating rating = Rating.EA;
        System.out.println(rating);
        System.out.println(rating.getRate());
        System.out.println(Rating.EA.getRatingClass());
        System.out.println(Rating.EM.getRatingClass());
        System.out.println(Rating.MA.getRatingClass());
        System.out.println(Rating.MN.getRatingClass());
    }
}

Output

EA
10
Above Average
Average
Average
Below Average

When we see the Rating.java, getRatingClass() is commonly written for all Rating Enum type as well it has been specifically re-written for EA and MN Rating type. So based on enum type the corresponding methods will be called.

We will discuss more about Java Enum in my next blogs.

 

Leave a comment

Your email address will not be published. Required fields are marked *