Enums in JAVA

by | Dec 18, 2020 | Java

Home » Java » Enums in JAVA

Introduction

Java enum stands for enumeration. This was first brought to effect in the JAVA 5 update. Enums are a keyboard in JAVA that is used to describe a collection of constants in a program. Enums reciprocate the working of a class. It can be thought of as a special class that can contain conventional class components such as methods, constants, etc. To define an enum class, the ‘enum’ keyboard has to be attached in the definition. Examples of enum declarations are given below.

Enums find the best use when all the possible outcomes are known to the developer. Enums act as constant values across their scope. However, this does not mean that the values cannot be overwritten. Unlike C/C++ enums, Java enums can have variables, methods, and constructors thereby making it more powerful by associating a behavior to it.

JAVA Enum Syntax

Enumerations in Java can be declared both inside or outside a class. The only restriction to its declaration is that it cannot be declared inside a method as a method does not generally have such a large scope of a class.

Let us look at two examples where JAVA Enum has been declared inside and outside a class.

  • Outside a class

    enum Pizza
    {
    CHEESE, CHICKEN, JALAPENO
    }
    public class Topping
    {
    public static void main (String [] args)
    {
    Pizza t = Pizza.CHEESE;
    out.println (“Topping chosen is” + t);
    }OUTPUT: CHEESE
  • Inside a class

    public class Topping
    {
    enum Pizza
    {
    CHEESE, CHICKEN, JALAPENO
    }
    public static void main (String [] args)
    {
    Pizza t = Pizza.CHEESE;
    out.println (“Topping chosen is” + t);
    }OUTPUT: CHEESE

Example

//This example is a simple Switch Case program that will extend the use of Enums as described //earlier.
import java.util.Scanner;
enum Pizza
{
      CHEESE, CHICKEN, JALAPENO
}
public class Sample
{
Pizza top;
//Constructor
public Sample (Pizza top)
{
this.top = top;
}
//Implementing switch case
public void pizzaTopping ()
{
switch (top)
{
case CHEESE:
System.out.println (“The Topping you have chosen in Cheese”);
break;
case CHICKEN:
System.out.println (“The Topping you have chosen in Chicken”);
break;
case JALAPENO:
System.out.println (“The Topping you have chosen in Jalapeno”);
break;
default:
System.out.println (“No toppings chosen. Try again”);
break;
}
}
public static void main (String [] args)
{
String str = “CHEESE”;
Sample flag = new Sample (Pizza.valueOf(str));
flag.pizzaTopping ();
}
}

 

Various Methods of the JAVA Enum class

To be able to use the methods mentioned below, one will need to import the java.lang.Enum  package.

  • values () – This method returns the value of all the constants that encapsulate the JAVA Enum.
  • ordinal () – The ordinal () method returns the index number of each enum as if the enums were data structures like arrays. It is used to establish a hierarchy of sorts.
  • valueOf () – If exists, this method returns the enum constant of a string value.

Let us see an example where all of these methods are put to use in an Enum.

enum Pizza
{
      CHEESE, CHICKEN, JALAPENO
}
public class Demo
{
public static void main (String [] args)
{
//Assigning the array with enum values
Pizza arr [] = Pizza.values();
for(Pizza top : arr)
{
//To print out the index numbers of each enum
System.out.println(top + “index no:” + top.ordinal());
//Finally, we will return the value of any one enum using the valueof () method
System.out.println (Pizza.valueOf (“CHICKEN”) );
}
}

 

OUTPUT

CHEESE index no: 0
CHICKEN index no: 1
JALAPENO index no: 2
CHICKEN

Difference between a class and JAVA Enum

Although Java Enum, just like a class can have methods and variables, enum members have to be public, static, and final. In classes, users have the freedom to keep the scope of members flexible.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author