In this article, you are going to learn more about java. Java enum is a very fascinating topic. I was not aware of it unless I encounter the problem. I have written couple articles on that like PostgreSQL and spring data JPA enum handling and database also support enum functionality.
techwasti.com/java-enum-mapping-in-postgres..
techwasti.com/understand-postgresql-enum-type
The enum is actually a class in java but with certain special features. Enum you can use when you have a set of values, like a week in Days, Months, or manage the state of the product.
A constructor of enum should be private and default only. We can’t make it public or protected.
Examples of TV operating systems are available, like android, apple, and WebOS.
public enum TVOS{
ANDROID("Android"), APPLE("apple"), WEBOS("WebOS");
String os;
TVOS (String os){
this.os=os;
}
}
Enum is a special class of java.
When I say enum is a java class so whatever you can do with class most of the things you can do with java enum. Enum represents a group of constants.
Create enum:
To create an enum, you have to use keyword enum.
enum Quality{
LOW,
MEDIUM,
HIGH
}
To access enum use the dot operator:
Quality varTest = Quality.MEDIUM;
Enum is like a data holder of constants and you can not change its values that's why its constructor is private.
One more way you can get the values:
public String getOs(){
return this.os;
}
If you add the above method to the enum then you will be able to access the values from outside the enum. Each and every method inside the enum is a static method. Check the below methods syntax.
Methods : name(), ordinal(), values(), valueOf()
name():
This is used to get an Enum name like WEBOS.name() it would return WEBOS as a string.
int ordinal():
This will return position of enum value ANDROID. ordinal() = 0, APPLE.ordinal() =1, WEBOS.ordinal() =2. This is more like to set the priority.
values(): This will return all enum values.
TVOS[] listOs= TVOS.values();
for(TVOS os : listOs) {
System.out.println(os);
}
valueOf():
This method converts String to enum.
Enum inside a class, you can define the enum inside the class as well. Let us say you have products and you want to define the quality of product as a enum.
public class Product {
enum Quality {
LOW,
MEDIUM,
HIGH
}
public static void main(String[] args) {
Quality varTest =Quality.MEDIUM;
System.out.println(varTest);
}
}
You can use an enum with the switch as well:
enum Quality {
LOW,
MEDIUM,
HIGH
}
public class Main {
public static void main(String[] args) {
Quality varTest =Quality.MEDIUM;
switch(varTest ) {
case LOW:
System.out.println("Low level");
break;
case MEDIUM:
System.out.println("Medium level");
break;
case HIGH:
System.out.println("High level");
break;
}
}
}
Let us learn some advanced things for enum:
Enum equals:
Enum contains static values we can use ==, .equals, or deepEquals() to compare two enum values. This will return either true or false.
How do we store enums of different types in the same type collection? Let us consider an example like days, months enum
public enum Days{
TUESDAY;
}
public enum MONTHS{
JUNE;
}
You can use this using the collection:
Collection<Enum<?>> list=new ArrayList<>();
list.add(Days.TUESDAY);
list.add(MONTHS.JUNE);
Map<Enum<?>,Integer> map=new HashMap<>();
Enum is mostly used for filtering purposes in real-life applications. You can decide something based on filters by using the list.contains() and map.containsKey() How do we convert String to its enum value generically?
one way to do this is by MobileOS.valueOf(String) but this is not generic
Enum inheritance: Enum is a special class, but it is not supporting inheritance. Enum can't have an enum extend another enum, and you cannot add values to an existing enum through inheritance. Enum can implement interface only.
e.g
interface Product{ }
public enum ElectronicProduct implements Product{}
Let us take one more image to understand JDK's existing API enums.
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
==========================**=========================
If this article adds any value for you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.