Java Enum Constructor

Summary: In this tutorial, you will learn how to use a Java enum constructor to associate data with enum constants.

Introduction to the Java enum constructor

Sometimes, you want to associate data with enum constants. For example, the following defines an enum called RGBColor with three constants: RED, GREEN, and BLUE:

enum RGBColor { RED, GREEN, BLUE }Code language: Java (java)

Typically, each color is represented as a combination of three integers: red, green, and blue. These integers define the intensity of each color component. They range from 0 to 255.

For example, RED should have red 255, green 0, and blue 0. Similarly, GREEN should have red 0, green 255, and blue 0; and BLUE should have red 0, green 0, and blue 255.

To associate these numbers to each constant in the RGBColor enum, you can define fields red, green, and blue in the enum and create a constructor that initializes them:

enum RGBColor {
    RED(255, 0, 0),
    GREEN(0, 255, 0),
    BLUE(0, 0, 255);

    private final int red;
    private final int green;
    private final int blue;

    RGBColor(int red, int green, int blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public int getRed() {
        return red;
    }

    public int getGreen() {
        return green;
    }

    public int getBlue() {
        return blue;
    }

    @Override
    public String toString() {
        return String.format("#%02X%02X%02X", red, green, blue);
    }
}Code language: Java (java)

How it works.

First, define three private fields: red, green, and blue:

private final int red;
private final int green;
private final int blue;Code language: Java (java)

Second, define a constructor that accepts three integers and assigns them to the corresponding red, green, and blue fields:

RGBColor(int red, int green, int blue) {
    this.red = red;
    this.green = green;
    this.blue = blue;
}Code language: Java (java)

Because each constant of the RGBColor enum is an instance of the enum, you need to initialize the three fields red, green, and blue as defined in the constructor.

Third, modify the RED, GREEN, and BLUE constants to include the values of three integers:

RED(255, 0, 0),
GREEN(0, 255, 0),
BLUE(0, 0, 255);Code language: Java (java)

Finally, define getter methods that return red, green, and blue values and override the toString() method that returns the hex code of the color:

public int getRed() {
    return red;
}

public int getGreen() {
    return green;
}

public int getBlue() {
    return blue;
}

@Override
public String toString() {
    return String.format("#%02X%02X%02X", red, green, blue);
}Code language: Java (java)

The following program shows how to use the RGBColor enum:

public class App {
    public static void main(String[] args) {
        var myColor = RGBColor.RED;

        System.out.println("Selected Color: " + myColor);
        System.out.println("Red Value: " + myColor.getRed());
        System.out.println("Green Value: " + myColor.getGreen());
        System.out.println("Blue Value: " + myColor.getBlue());
        System.out.println("Hex Code: " + myColor);

    }
}Code language: Java (java)

Output:

Selected Color: #FF0000
Red Value: 255
Green Value: 0
Blue Value: 0
Hex Code: #FF0000Code language: Java (java)

Summary

  • Use a Java enum constructor to associate data with constants of the enum.
Was this tutorial helpful ?