What are Enums in Java?
In computer languages, enumerations are used to express a collection of named constants. For instance, it can be used for weekdays, colors, and vowels.
Enums are employed when all possible values are known at the time of compilation, as in the case of menu options, rounding modes, command-line inputs, etc. The list of constants for an enum type need not remain consistent throughout time.
Java Enum inherently derives from the Enum class; as a result, it is unable to inherit any other classes, albeit it can implement numerous interfaces. Fields, constructors, methods, and main methods are all conceivable with Java enums.
Declaration of enum in Java:
Enum declaration can be done outside a Class or inside a Class, but not inside a Method.
For instance,
enum Color {
RED,
GREEN,
BLUE;
}
Important Enumeration Points:
- Internally, each enum is implemented using a Class.
- An object of the type enum is represented by each enum constant.
- Switch statements accept arguments of the enum type.
Example:
import java.util.Scanner;
// An Enum class
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
public class Coderz
{
Day day;
// Constructor
public Coderz(Day day)
{
this.day = day;
}
// Prints a line about Day using switch
public void dayIsLike()
{
switch (day) {
case MONDAY:
System.out.println("Mondays go to coderz.");
break;
case FRIDAY:
System.out.println("Fridays go to rest.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days");
break;
}
}
// Driver method
public static void main(String[] args)
{
String s = "MONDAY";
Coderz obj = new Coderz(Day.valueOf(s));
obj.dayIsLike();
}
}
Output:
Mondays go to coderz.
What function does the enum’s values() method serve?
When an enum is created, the Java compiler internally adds the values() function. All the enum’s values are returned in an array via the values() method.
What does the enum’s function valueOf() method accomplish?
When it builds an enum, the Java compiler automatically includes the valueOf() function. The value of the specified constant enum is returned by the function valueOf() function.
What function does the enum’s ordinal() method serve?
When it builds an enum, the Java compiler internally inserts the ordinal() function. The index of the enum value is returned by the ordinal() method.
Java program to demonstrate working of values(), ordinal() and valueOf()
enum Color {
RED,
GREEN,
BLUE;
}
public class Coderz {
public static void main(String[] args)
{
// Calling values()
Color arr[] = Color.values();
// enum with loop
for (Color col : arr) {
// Calling ordinal() to find index
// of color.
System.out.println(col + " at index "
+ col.ordinal());
}
System.out.println(Color.valueOf("RED"));
// System.out.println(Color.valueOf("WHITE"));
}
}
Output:
RED at index 0
GREEN at index 1
BLUE at index 2
RED
Initializing specific values to the enum constants:
The initial values for enum constants range from 0 to 1, 2, 3, and so forth. But by specifying fields and constructors, we may initialize a specific value for the enum constants. Enum can have fields, constructors, and methods, as was previously mentioned.
Example:
// A Java program to demonstrate working on enum
class Coderz{
enum Season{ // An Enum class
WINTER(5), SPRING(10), SUMMER(15), FALL(20);
private int value;
private Season(int value)//constructor
{
this.value=value;
}
}
public static void main(String args[])
{
//rinting the values of the seasons
for (Season s : Season.values())
System.out.println(s+" "+s.value);
}
}
Output:
WINTER 5
SPRING 10
SUMMER 15
FALL 20
Note: also read about the Java.lang.ThreadGroup- class in Java
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment