Enum (enumeration type)

What is Enum (enumeration type)?

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.

About constants

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

Enum (enumeration type)
[Swift] Type type ~ Enumeration type ~
[Java] Enumeration type
[Java] Express Enum type without using Enum (enumeration) type
[Swift] Shared enumeration type
[Java] About enum type
How to use Java enum type
enum definition
Pass type
About enum
[Java] enum
Directory type
[Practice] Enumeration
Try to sort classes by enumeration type
Human type