Set Value to Enum in Java
In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to Enum in java along with example programs
Let us first know what is Enum.
Enum
A data type in Java called Enum has a predetermined set of constants.
Java Enums can indeed be compared to classes that contain a predetermined collection of constant (a constant: a variable that never changes). Implicitly, the Java enum values are static and final. It has been accessible since JDK 1.5.
Enums are employed to design custom data types, much like classes. In Java, an enum is defined using the enum data type, also referred to as the Enumerated Data Type. Enum in Java is much more versatile than it is in C/C++. Here, we have the option of defining an enum either within or outside of the class.
Let us discuss a simple example program for Java Enum
File name: Enum.java
class Enum
{
// the class's definition of the enum
public enum Subject { Java, DBMS, Python, HTML }
public static void main(String[] args)
{
// going through the enum
for (Subject s : Subject.values())
System.out.println(s);
}
}
Output
Java
DBMS
Python
HTML
Note: At compile time, the Java compiler dynamically adds the values(), function valueOf(), as well as ordinal() methods in within enum. For the enum, it internally produces a static but also final class.
Enums typically contain its unique string values, but we can also give them some unique values. Take the example below as evidence.
Example
enum Planets
{
Earth(“Blue”), Mars(“Red”), Saturn(“Yellow”);
}
With their own unique custom values of Blue, Red, and Yellow, respectively, Earth, Mars, and Saturn are the three members of the Planets enum in the example above.
There are a few guidelines we must adhere to in order to implement this enum in code:-
- For this enum class, a parameterized constructor must be created. Because, Given that we are aware that the objects of the enum class cannot be created explicitly, we utilize the parameterized constructor to initialize the objects. Additionally, the constructor had to have private or basic modifiers and cannot be public or protected. Why? We can initialize many objects if we construct public or protected. This completely violates the enum idea.
- To determine the value of enums, single getter method must be created.
We updated the public interface of our enum by including fields and functions. As a result, the fundamental Enum name() and function valueOf() methods used in our code aren't aware of the new fields.
We cannot supply our own function valueOf() implementation because the Java language has already defined the static function valueOf() method for us.
Similarly, we are unable to alter the Enum.name() method because it is final.
Enum values are only repeated several times once as a result of caching, which simplifies the valueOfLabel() method.
As an option in contrast, we can construct the cache asynchronously when the valueOfLabel() method accesses it for the first time. In that instance, synchronising map access is necessary to avoid concurrent issues.
As a result, utilising the regular Enum API to access our additional data is not a viable option. Instead, let's examine some alternative methods for making our fields visible.
Let us go through a simple example program
File name: TrafficSignal.java
// Program written in Java to show how values can be allocated to enums.
enum TrafficSignal
{
// The enum constructor will be invoked with a single String argument.
RED("STOP"), GREEN("GO"), ORANGE("SLOW DOWN");
// defining a private variable and retrieving its values
private String action;
// getter approach
public String getAction()
{
return this.action;
}
// Enum constructors cannot be shielded or made public.
private TrafficSignal(String action)
{
this.action = action;
}
}
public class EnumConstructorExample
{
public static void main(String args[])
{
// Let's display the description of the each enum and an explanation of its action: Enum //values()
TrafficSignal[] signals = TrafficSignal.values();
for (TrafficSignal signal : signals)
{
// To obtain the value, utilise the getter method.
System.out.println("name : " + signal.name() +
" action: " + signal.getAction() );
}
}
}
Output
name : RED action: STOP
name : GREEN action: GO
name : ORANGE action: SLOW DOWN