20 Jul Java Enums
We studied some types of Java. However, there is another type of Java called Enums. Enums stand for enumerations, i.e., a group of constants. Use enums to name constants.
We use the enum keyword to create an enum. Let us see the syntax:
enum enum_name {
enum_item1,
enum_item2,
enum_item3,
.
.
.
enum_itemn
}
Above, we have the enum name after the enum keyword. Then come the enum items, i.e., constants separated by a comma.
Create an Enum
Let us now see how to create an enum. We will consider Directions and enum items as NORTH, SOUTH, etc.
enum Directions {
NORTH,
SOUTH,
EAST,
WEST
}
Create an Enum Variable
Create a variable of the Enum if you want to access it. Let us create a variable of the above enum:
Directions d1;
Assign a value to the enum variable
Access enum constants/ items with a dot. Let us now assign a value to an enum item and display. Here is our first example to create an enum with enum variable, and assign value:
// Create an Enum, Enum variable, and assign a value to the variable in Java
enum Directions {
NORTH,
SOUTH,
EAST,
WEST
}
public class Demo112 {
public static void main(String[] args) {
// Create an Enum variable and assign value
Directions d1 = Directions.NORTH;
System.out.println(d1);
}
}
Output
NORTH
Create an Enum inside a class
Let us see an example of creating an enum inside a class:
class Studyopedia {
enum Directions {
NORTH,
SOUTH,
EAST,
WEST
}
public static void main(String[] args) {
// Enum variables
Directions d1, d2, d3, d4;
// Assign valuee to Enum variables
d1 = Directions.NORTH;
d2 = Directions.SOUTH;
d3 = Directions.EAST;
d4 = Directions.WEST;
// Display
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
}
}
Output
NORTH SOUTH EAST WEST
Display the Enum item names with the ordinal (position) values
To display the enum items, we will use the for each loop with the values(), name(), and ordinal() built-in Java functions:
// Create an Enum in a Class in Java
class Demo113 {
enum Directions {
NORTH,
SOUTH,
EAST,
WEST
}
public static void main(String[] args) {
// Enum variables
Directions d1, d2, d3, d4;
// Assign the value to Enum variable
d1 = Directions.NORTH;
d2 = Directions.SOUTH;
d3 = Directions.EAST;
d4 = Directions.WEST;
// Print
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
// Display the enum item name with the ordinal values
// name() returns the exact name of the enum constant as declared
// ordinal() returns the position index
for (Directions d : Directions.values()) {
System.out.println("Name = " + d.name() + ", Ordinal = " + d.ordinal());
}
}
}
Output
NORTH SOUTH EAST WEST Name = NORTH, Ordinal = 0 Name = SOUTH, Ordinal = 1 Name = EAST, Ordinal = 2 Name = WEST, Ordinal = 3
Create a custom function in Enum
Let us now see how to create a custom function in the Enum itself. We will use it to display the name and ordinal values:
// Create a custom function in Enum
class Demo114 {
enum Directions {
NORTH,
SOUTH,
EAST,
WEST;
void display() {
System.out.println("Name = " + this.name() + ", Ordinal = " + this.ordinal());
}
}
public static void main(String[] args) {
Directions d1, d2, d3, d4;
// Assign the value to Enum variable
d1 = Directions.NORTH;
d2 = Directions.SOUTH;
d3 = Directions.EAST;
d4 = Directions.WEST;
// Use display()
d1.display();
d2.display();
d3.display();
d4.display();
}
}
Output
Name = NORTH, Ordinal = 0 Name = SOUTH, Ordinal = 1 Name = EAST, Ordinal = 2 Name = WEST, Ordinal = 3
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments