[Java] enum

This time I had a chance to use enum in java, so I investigated it.

Declaration method

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
}

How to use values

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;

methods of enum

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

[Java] enum
Java Enum utilization example
Enum reverse map Java
Java
[Java] About enum type
Java
Enum Strategy pattern in Java
Reproduce Java enum in C #
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
[Java] Branch enum with switch statement
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
About enum
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)