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.
Enum declaration can be done outside a Class or inside a Class, but not inside a Method.
For instance,
enum Color {
RED,
GREEN,
BLUE;
}
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();
}
}
Mondays go to coderz.
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.
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.
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.
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"));
}
}
RED at index 0
GREEN at index 1
BLUE at index 2
RED
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.
// 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);
}
}
WINTER 5
SPRING 10
SUMMER 15
FALL 20
Note: also read about the Java.lang.ThreadGroup- class in Java
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…