Java - Enums | Enumeration With Example


The Java programming language contains the enum keyword, which represents a special data type that enables for a variable to belong to a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. 

The values defined inside an enum are constants. Also, these values are implicitly static and final and cannot be changed, after their declaration. If an enum is a member of a class, then it is implicitly defined as static. Finally, you should use an enum, when you need to define and represent a fixed set of constants.

class Test 

        enum E 
                a, b, c, d; 
        public static void main(String[] args) 
                E e1 = E.b; 
                E e2 = E.d; 
                System.out.println(e1.ordinal());//ordinal() is used to get the position of that enum 


Enums in Java are considered to be reference types, like class or Interface and thus, a programmer can define constructor, methods and variables, inside an enum 

An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. Apex provides built-in enums, such as LoggingLevel, and you can define your own enum. 

All Apex enums, whether user-defined enums or built-in enums, have the following common method that takes no arguments. 


This method returns the values of the Enum as a list of the same Enum type. Each Enum value has the following methods that take no arguments. 


Returns the name of the Enum item as a String. 


Returns the position of the item, as an Integer, in the list of Enum values starting with zero. 


class Test 
        enum Month 
        { JAN(31), FEB(28), MAR(31); 
            int days; 
            Month(int days) 
                    this.days = days; 
            int getDays() 
                    return days; 
    public static void main(String[] args) 
        Month m1 = Month.FEB; 
        Month m2 = Month.JAN; 



"what is enumeration in java with example"

"what is enumeration in java collections with example"

"iterator and enumeration in java with example"

"what is enumeration explain with example in java"

"use of enumeration in java with example"

"java create enumeration from list"

"what is enumeration explain with example"

"explain enumeration in java"

"enumeration in java"

"enum with values in java"

"enumeration in java collection"

"enum with multiple values in java"

"java enum with string values"

"java enum with int values"