Java - Enum datatype

1 - About

The enum data type implementation in java

Because they are constants, the names of an enum type's fields are in uppercase letters.

The enum declaration defines a class (called an enum type). The enum class body can include:

  • methods
  • and other fields.

3 - Example

  • compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
  • days of the week

4 - Definition

An enum type is defined by using the enum keyword.

public enum Day {

5 - Instantiation

An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type

Enum types may not be instantiated. The following code will throw an error:

MyEnum myEnum = new MyEnum();

6 - Method associated

The compiler automatically adds some special methods when it creates an enum.

  • a static values method that returns an array containing all of the values of the enum in the order they are declared.
  • a static ordinal method that returns the numerical position of each enum constant in its type

7 - Conversion

7.1 - toOrdinal


7.2 - Int to Enum


7.3 - To joined string
    .collect(Collectors.joining(", "))

7.4 - String to enum

EnumClass.valueOf("An Enum String")

7.5 - enum to String

7.6 - Stream of String to Array

Transform a list of string in an enum array

MyEnum[] myEnums=

8 - Example

8.1 - Enum

public enum SimpleEnum {

    NICO("nico", 41.0),
    MADELIEF("madelief", 8.0);

    private final String name;
    private final Double age;

    SimpleEnum(String name, Double age) { = name;
        this.age = age;

    public static void main(String[] args) {
        // Get a Madelief object, print Nico
        SimpleEnum simpleEnum = SimpleEnum.MADELIEF;
        System.out.println("Print Nico as an instance method of Madelief: "+simpleEnum.getDynamicallyNico());

        // Print a static
        System.out.println("Print Nico as class method (Statically): "+SimpleEnum.getStaticallyNico());

        // Print Madelief and Nico
        System.out.println("Print all enum values ");
        for (SimpleEnum enumValue : SimpleEnum.values()) {
            System.out.printf("The age of %s is %f%n",
                    enumValue, enumValue.age);


    public static SimpleEnum getStaticallyNico() {
        return NICO;

    public static SimpleEnum getDynamicallyNico() {
        return NICO;

Print as instance method NICO
Print as class method (Statically) NICO
Print all enum values 
The age of NICO is 41.000000
The age of MADELIEF is 8.000000

8.2 - Singleton

Singleton implementation.

INSTANCE is public static final field that represents the enum instance. You can get the instance of the class with the following statement EnumSingleton.INSTANCE but it's best to encapsulate it in a getter in case of you may want to change the implementation.

public enum EnumSingleton {


    private String name; // Mandatory
    private Double age = null; // Not Mandatory

    private void build(SingletonBuilder builder) { =;
        this.age = builder.age;

    // Static getter
    public static EnumSingleton getSingleton() {
        return INSTANCE;

    public void print() {
        System.out.println("Name "+name + ", age: "+age);

    public static class SingletonBuilder {

        private final String name;
        private Double age = null;

        private SingletonBuilder(){
              name = null;

        SingletonBuilder(String name) {
   = name;

        public SingletonBuilder age(double age) {
            this.age = age;
            return this;

        public void build(){


    public static void main(String[] args) {
        new SingletonBuilder("nico").age(41).build();


Name nico, age: 41.0

9 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap