[Java] About enum type

Programming study diary

December 29, 2020 Here's a quick summary of how to work with enums in Java using enums.

What is an enum type?

A type that can combine multiple constants into one. Handles data that cannot be changed later because it is a constant. The ennum type has the advantage that multiple constants can be grouped together.

The ~~ enum type has the advantage of making switch statements easier to handle. ~~ → Since the branch is completed when the instance of enum is acquired, it is good to give the process and value that you want to branch with the switch to the enum itself.

How to use enum type

The enum type is described as follows.

Basic writing


enum enum{Enumerator 1,Enumerator 2,・ ・ ・};

//When retrieving an enum
Enumeration name.Data you want to get

Sample code


public class Sample {
           public static void main(String[] args) {
                      //Get Name and output only Tanaka data in it
                      Name name = Name.Tanaka;
                      System.out.println(name);
           }
           //Define enum Name
           protected enum Name {
                      Tanaka,
                      Sato,
                      Kimura
           };
}

Execution result


Tanaka

Sample code using switch statement

public class Sample {
  public static void main(String[] args) {
    Name name = Name.Tanaka;

    switch(name) {
      case Tanaka:
        System.out.println("Tanaka");
        break;
      case Sato:
        System.out.println("Sato");
        break;
      case Kimura:
        System.out.println("Kimura");
        break;
      }
    }

    protected enum Name {
       Tanaka,
      Sato,
      Kimura
    };
}

Execution result


Tanaka

References

[For beginners] Explain Java Enum types! Introducing basic methods! How to use enums with enum in Java [for beginners]

Recommended Posts

[Java] About enum type
[Java] enum
How to use Java enum type
Java type conversion
About Java interface
[Java] About Java 12 features
Enum (enumeration type)
[JAVA] Stream type
[Java] About arrays
[Java] Enumeration type
Java Optional type
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
Java double type
About Java class
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
[Java] Express Enum type without using Enum (enumeration) type
[Java Siler] About type inference by var
[Basic knowledge of Java] About type conversion
About Java log output
About Java functional interface
Java, about 2D arrays
Java Enum utilization example
About class division (Java)
About Java StringBuilder class
[Introduction to Java] About type conversion (cast, promotion)
[Java] About Singleton Class
[Java] Data type ①-Basic type
About Java method binding
About var used in Java (Local Variable Type)
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
About inheritance (Java Silver)
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
All about Java programming
About java abstract class
[Java] Date type conversion
About Java basic data types and reference type memory
[Java] Things to note about type inference extended in Java 10
A note about Java GC
About an instance of java
Java learning memo (data type)
What I researched about Java 6
[Gradle] About Java plug-in tasks