It is a type that can put multiple constants together. The constant defined by Enum is called an enumeration type.
Enums are also used in C language, and there are some similarities in the constant method, but since java enums are classes, you can define fields and methods.
The advantage of using Enum is that the readability is improved by using the enumeration type in the switch statement.
The definition of Enum is described as follows.
Access modifier enum enumeration{Enum 1,Enum 2,};
Sample confirmation
public class Main {
public static void main(String[] args) {
Fruit fruit_type = Fruit.Orange;
System.out.println(fruit_type);
}
protected enum Fruit {
Orange,
Apple,
Melon
};
}
result
Orange
This sample code defines an enumeration Fruit. To use Fruit, declare an object like a normal class. Here we declare fruit_type.
You can store the Enum enum in the declared object and use the object.
Recommended Posts