This time I had a chance to use enum in java, so I investigated it.
Enums are defined in the same way as classes.
enum Enum name {
}
Based on that, I will list the fruits. Then, it becomes as follows.
enum Fruit {
APPLE,
ORANGE,
BANANA,
GRAPE; // Some samples have semicolons behind blocks or not
}
The value of enum can be accessed by enum name.value as well as access to static field.
System.out.println(Fruit.APLLE);
Of course, it is also possible to create a variable of enum name type and assign it.
Fruit apple = Fruit.APPLE;
enums have various methods. The typical ones are introduced below.
name() Returns the name of the enum value as a string.
System.out.println(Fruit.APLLE.name()); //"APPLE"
original() Returns the number in the order declared by the enum. Since APPLE is the 0th, 0 is returned.
System.out.println(Fruit.APLLE.original()); //0
Recommended Posts