Java Enum
Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond, Heart, and Spade that fall under the umbrella of the enumerated type Suit. Natural enumerated types are another illustration
When we know every potential value at compilation time, such as for menu options, rounded modes, command-line flags, etc., Enums are employed. An Enum type's set of constants need not always remain consistent over time.
A class type in Java is an enumeration. Although we don't need to use new to create an instance of an Enum, it can still perform the same functions as other classes. Enumeration in Java is a very effective tool because of this aspect. They can have constructors, instance variables, and methods added, just like classes, and you can even create interfaces.
Keep in mind that enumerations cannot inherit from other classes or be expanded, unlike classes (i.e. become superclass).
Enums are expressed in Java (as of 1.5) using the Enum data type. Enums in Java are more capable than those in C/C++. Variables, methods, as well as constructors, can all be added to it in Java. Enum's primary goal is to allow us to create custom data types (Enumerated Data Types).
Example 1
enumColor {
BLUE,
GREEN,
PINK;
}
public class Test {
public static void main(String[] args)
{
Color c1 = Color.BLUE;
System.out.println(c1);
}
}
Output
BLUE
Example 2
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.
import java.util.Scanner;
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
public class Test {
Day day;
public Test(Day day) { this.day = day; }
public void dayIsLike()
{
switch (day) {
case MONDAY:
System.out.println("Mondays are good.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
public static void main(String[] args)
{
String str = "MONDAY";
Test t1 = new Test(Day.valueOf(str));
t1.dayIsLike();
}
}
Output
Mondays are good.
Example 3
Every enum constant is inherently public static final. The enum Name can be used to access it because it is static. We cannot make child enums because it is final.
The main() method can be declared inside the enum. So, using the Command Prompt, we can just call an enum.
enumColor {
BLUE,
BROWN,
PINK;
public static void main(String[] args)
{
Color c1 = Color.BLUE;
System.out.println(c1);
}
}
Output
BLUE
Example 4
Enumerations and Inheritance
Implicitly, every enum extends Java.lang.
- class enum. An enum never extends anything else, just as a class in Java could only extend oneparent.
- In Java.lang, the function toString() { [native code] }() method is overridden.Theenum constant name is returned by the enum class.
- Many interfaces can be implemented with an enum.
The methods values(), ordinal(), and function valueOf()
- These methods can be found in the Java.lang library.
- All values contained within the enum can be returned using the enum.values() method.
- In enums, the order is crucial.
- Every enum constant index can always be retrieved utilizingthe ordinal() method, exactly like an array index.
- If an enum constant for the provided string value exists, the function valueOf() { [native code] }() method returns it.
enumColor {
PINK,
GREEN,
BLUE;
}
public class Test {
public static void main(String[] args)
{
Colorarr[] = Color.values();
for (Color col: arr) {
System.out.println(col + " at index "
+ col.ordinal());
}
System.out.println(Color.valueOf("PINK"));
}
}
Output
PINK at index 0GREEN at index 1
BLUE at index 2
PINK
Example 5
Function Object() and enumerator
Enums are capable of having constructors, which are run independently for every enum constant when the enum class is loaded.
Enum objects cannot be explicitly created, and as a result, their function Object() { [native code] } cannot be called directly.
enum and procedures:
Enum is capable of including both concrete and abstract procedures. Every instance of an enum class that has an abstract data type must implement it.
enumColor {
RED,
GREEN,
BLUE;
private Color()
{
System.out.println("The Constructor called for : "
+ this.toString());
}
public void colorInfo()
{
System.out.println("Universal Color");
}
}
public class Test {
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
c1.colorInfo();
}
}
Output
Constructor called for: REDConstructor called for: GREEN
The constructor called for: BLUE
RED
Universal Color