In the following example you’ll see how to add a constructor to an enum type value. Because an enum is just another class type it can have constructors, fields and methods just like any other classes. Below we define a constructor that accept a string value of color code. Because our enum now have a new constructor declared we have to define the constant named value as RED("FF0000")
, ORANGE("FFA500")
, etc.
In Java enumeration expanded beyond just as a named constants. Because enum
is a class type we can add methods, fields and constructors to the enum type as you can see in the example below.
package org.kodejava.basic;
public enum Rainbow {
RED("FF0000"),
ORANGE("FFA500"),
YELLOW("FFFF00"),
GREEN("008000"),
BLUE("0000FF"),
INDIGO("4B0082"),
VIOLET("EE82EE");
private final String colorCode;
// The constructor of Rainbow enum.
Rainbow(String colorCode) {
this.colorCode = colorCode;
}
/**
* Get the hex color code.
*
* @return hex color code
*/
public String getColorCode() {
return colorCode;
}
}
package org.kodejava.basic;
public class EnumConstructor {
public static void main(String[] args) {
// To get all values of the Rainbow enum we can call the
// Rainbow.values() method which return an array of
// Rainbow enum values.
for (Rainbow color : Rainbow.values()) {
System.out.println("Color = " + color.getColorCode());
}
}
}
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023