#
Enum Class in Java
This tutorial explains the Enum Class in Java.
Info
The Enum Class in Java is a Wrapper Class for a list of predefined objects. Each object is located on a particular memory address and all the internal objects are created in the same time.
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
---------------------------
Conclusions
- All predefined objects (AUDI, BMW, PEUGEOT) are created by a constructor before the usage. All these objects
have a different location (hashCode() is "by address" (we are working with Objects) not "by value", because each time we
have different values). To be sure 100% we can use
System.identityHashCode()
as well. - If the constructor is not created in the code, it will be created under the hood using a minimal implementation.
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
---------------------------
Conclusions
- we can define different attributes/properties and methods for the internal objects.
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);
}
}
- If the method defined of the object level has another name that the ones defined at the enum class level, that method cannot be accessed from car1 object as this is a reference of a "enum" type.
- We can implement interfaces in Enum Classes. Please take a look of the following 2 articles: Enum implements an Interface I Enum implements an Interface II