Java Enum Keyword
Definition: A data type in Java called Enum has a respect to supply of constants.
The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST), seasons (SPRING, SUMMER, WINTER, and AUTUMN), and colors (R(red), Y(yellow), B(Black), G(green), W(white), and B(blue)), among other things, can all be executed on it. The Java naming conventions imply that we should initial each constant. Therefore, we have emphasized Enum constants.
Java Enums can be differentiated with the classes that contain a derived collection of constants (a variable that does not change). It provides static and final reserved words are the java constants using Enum. 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 more powerful than it is in C/C++. Here, we have the option of defining an Enum either within or outside of the class.
Java Enum is internally derived from the Enum class; therefore, it is unable to descend from any other class, yet it can implement numerous interfaces. The Enum keyword in java programming language consists of fields, constructors, methods, and primary methods.
Observations for Java Enum
- Enhancing type safety with Enum.
- Enum is simple to use in switches.
- Traversing Enum is possible.
- Fields, constructors, and methods can be discovered or created in Enum keyword.
- Enum can implement numerous interfaces, but it can only internally extend the Enum class.
Simple Examples of Java using Enum
Example 1:
Simple Example programfor Java Enum
class EnumEx1{
public enum c_son{WINTER, SPRING, SUMMER}
//main method
public static void main (String [] s) {
//executing the Enum
for (c_son t:c_on. values())
System.out.println(t);
}}
Output
WINTER
SPRING
SUMMER
Example 2:
Let's look at another Java enum example using the value (), function valueOf() {[native code]} (), and ordinal () methods.
class EnumEx2{
//declaring enum within class
public enum c_son {WINTER, SPRING, SUMMER}
//main method is developed
public static void main (String [] s) {
//executing all enum
for (Season t:Season. Values ()) {
System.out.println(t);
}
System.out.println("winter value is: "+Season.valueOf("WINTER"));
System.out.println("winter index is: "+Season.valueOf("WINTER"). ordinal ());
System.out.println("summer index is: "+Season.valueOf("SUMMER"). ordinal());
}}
Output
WINTER
SPRING
SUMMER
winter value is: WINTER
winter index is: 0
summer index is: 2
What function does the Enum's values () method serve?
When an Enum is created, the Java compiler internally adds the values () function. All of the Enum's values are provided in an array via the values () method.
What does the Enum's function valueOf () {[native code]} () method accomplish?
When it builds an Enum, the Java compiler automatically includes the function valueOf() {[native code]} () function. The value of the specified constant Enum is returned by the function valueOf() {[native code]} () function.
What function does the Enum'sordinal () 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.
Defining Java Enum
Within or outside the class the Enum can be declared because it is like a class. The semicolon (;) at the end of the enum constants are optional.
For example:
enum C_son{WINTER, SPRING, SUMMER;}
enum C_son{WINTER, SPRING, SUMMER;}
The above declared syntax of declaring the enum are same.
Example program of enum outside the class:
enum C_son {WINTER, SPRING, SUMMER}
class EnumEx2{
public static void main (String [] s) {
C_sonC=C_son. WINTER;
System.out.println(C);
}}
Output:
WINTER
Example program of enum inside the class:
class EnumEx3{
enum c_son{WINTER, SPRING, SUMMER, FALL;}//Here we use semi colon as optional
public static void main (String [] s) {
c_sonc=c_son. WINTER;//
System.out.println(c);
}}
Output:
WINTER
Example program for main () method inside the enum:
When enum is inserted or imported in a main () method then it can run the enum directly.
enum Sea {
WINTER, SPRING, SUMMER;
public static void main (String [] s) {
Sea c=Sea.WINTER;
System.out.println(c);
}
}
Output:
WINTER
Example program for applying Enum on a switch statement:
class EnumEx5{
enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}
public static void main (String args []){
Day d=Day.MON.
switch(d) {
case SUN:
System.out.println("sunday");
break;
case MON:
System.out.println("monday");
break;
default:
System.out.println("Any day of the week");
}
}}
Output of the program is given below:
monday