# Enum Class in Java

In 
Published 2024-01-06

This tutorial explains the Enum Class in Java.

Take a look at the following code:

"the enum class"
package com.example.demo;

public enum MyCars {
    AUDI,
    BMW,
    PEUGEOT;

    MyCars(){
        System.out.println("Constructor >> " + this.name() + " - " + this.hashCode());
    }
}
"the main class"
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);

		System.out.println("---------------------------");

		MyCars car1 = MyCars.AUDI;
		System.out.println("car1.name() = " + car1.name());

		System.out.println("---------------------------");
	}
}

When I run the code I receive:

---------------------------
Constructor >> AUDI - 1933328958
Constructor >> BMW - 1264754451
Constructor >> PEUGEOT - 1888639813
car1.name() = AUDI
---------------------------

An interesting thing is that we can define attributes & methods for these internal objects.

Take a look at the following code:

"the enum class"
package com.example.demo;

public enum MyCars {
    AUDI ("yellow", 30),
    BMW("green", 2),
    PEUGEOT("white", 10);

    String color;
    Integer ageInMonths;

    MyCars(String color, Integer ageInMonths){
        this.color = color;
        this.ageInMonths = ageInMonths;

        System.out.println("Constructor >> " + this.name() + " - " + this.hashCode() + "// color= "+this.color);
    }

    void getMyColor(){
        System.out.println("My color is: "+this.color);
    }
}
"the main class"
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);

		System.out.println("---------------------------");

		MyCars car1 = MyCars.AUDI;
		car1.getMyColor();

		System.out.println("---------------------------");
	}
}

When I run the code I receive:

---------------------------
Constructor >> AUDI - 1611221523// color= yellow
Constructor >> BMW - 439232821// color= green
Constructor >> PEUGEOT - 1933328958// color= white
My color is: yellow
---------------------------

The enum class could be written like bellow if we need to override some methods only for some particular "internal objects":

"the enum class"
package com.example.demo;

public enum MyCars {
    AUDI ("yellow", 30) {
        void getMyColor(){
            System.out.println("My color is >> "+this.color + " <<");
        }
    },
    BMW("green", 2),
    PEUGEOT("white", 10);

    String color;
    Integer ageInMonths;

    MyCars(String color, Integer ageInMonths){
        this.color = color;
        this.ageInMonths = ageInMonths;

        System.out.println("Constructor >> " + this.name() + " - " + this.hashCode() + "// color= "+this.color);
    }

    void getMyColor(){
        System.out.println("My color is: "+this.color);
    }
}