×

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

Related Topics

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.