×

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

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 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.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.