Friday, 13 July 2018

Java Enum With Constructor Example

Java Enum with Constructor

Many Java developers don't know that Java Enum can have a constructor to pass data while creating Enum constants. This feature allows you to associate related data together. One example of passing arguments to enum Constructor is our TrafficLight Enum where we pass action to each Enum instance e.g. GREEN is associate with go, RED is associated with stop and ORANGE is associated with the slow down. This is really useful because it provides more context and meaning to your code. If you want, you can also provide one or more constructor to your Enum as it also supports constructor overloading like normal Java classes. This is very different from enum you have seen in C or C++, which is just a collection of fixed thing without any OOP power.

Oracle Java Tutorial and Materials. Oracle Java Learning, Oracle Java Certifications, Oracle Java Guides

Just remember that constructor in enums an only be either private or package level it can't be public or protected hence access modifier public and protected are not allowed to Enum constructor, it will result in compile time error.

By the way, this is not our first tutorial on Java Enum where we have explained a key feature, we have also ready covered some important features on Enum in our previous examples like Java Enum Switch Example explains that you can use enum constants inside switch block.

Java Enum with Constructor Example

Java Enum with constructor example tutorialHere is complete code example of using Constructor with Java Enum. Here is our TrafficLight constructor accepts a String argument which is saved to action field which is later accessed by getter method getAction().

As I explained, we have an Enum TrafficSignal, which have three enum constants, RET, GREEN, and ORANGE, and we have associated, wait, go and slow down with them by passing values into the constructor.

Oracle Java Tutorial and Materials. Oracle Java Learning, Oracle Java Certifications, Oracle Java Guides

 * Java enum with constructor for example.
 * Constructor accept one String argument action
public enum TrafficSignal{
    //this will call enum constructor with one String argument
    RED("wait"), GREEN("go"), ORANGE("slow down");
    private String action;
    public String getAction(){
        return this.action;
    // enum constructor - can not be public or protected
    TrafficSignal(String action){
        this.action = action;

 * Java Enum example with constructor. 
 * Java Enum can have constructor but it can not
 * be public or protected
 * @author
public class EnumConstructorExample{

    public static void main(String args[]) {
      //let's print name of each enum and there action
      // - Enum values() examples
      TrafficSignal[] signals = TrafficSignal.values();
      for(TrafficSignal signal : signals){
          //Java name example - Java getter method example
          System.out.println("name : " 
                             + " action: " 
                             + signal.getAction());

This was our Java Enum example with Constructor. Now, you know that Enum can have a constructor in Java which can be used to pass data to Enum constants, just like we passed action here. Though Enum constructor cannot be protected or public, it can either have private or default modifier only.

Related Posts