Enumeration
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;
System.out.println(e1);
E e2 = E.d;
System.out.println(e2);
System.out.println(e1.ordinal());//ordinal() is used to get the position of that enum
System.out.println(e2.ordinal());
System.out.println(E.valueOf("a").ordinal());
}
}
Output
b
d
1
3
0
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.
values
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.
name
Returns the name of the Enum item as a String.
ordinal
Returns the position of the item, as an Integer, in the list of Enum values starting with zero.
Example2:
{
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;
System.out.println(m1);
System.out.println(m1.getDays());
System.out.println(m1.days);
System.out.println("--------");
Month m2 = Month.JAN;
System.out.println(m2);
System.out.println(m2.getDays());
System.out.println(m2.days);
System.out.println("--------");
}
}
Output
FEB
28
28
--------
JAN
31
31
ALSO SEARCH:
"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"