1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaにおけるenum(列挙型)の使い方覚え書き

1
Last updated at Posted at 2026-01-19

javaを勉強し始めて詰まったことを覚え書きとして書き溜めていこうと思います。
今回はenum形についてです。

emum型は列挙型ともいわれる型で、あらかじめ決められた値の集合です。

emum_test.java
public class emum_test{
//宣言//
    enum Day {
        MON, TUE, WED, THU, FRI, SAT, SUN
    }
        
    public static void main(String[] args) {
            
        //Day型の変数dにMONを代入
        Day d = Day.MON;
    }
}

この場合はDay型の変数にはenum Day{}で定義した要素しか代入できません。

何が便利か

1. 定義時以外値を追加できないので配列に比べて安全
2. 定義外の値の参照をできないので配列に比べて安全
3. 状態の定義に便利
4. 複数の値、複数の型を保持でき、渡すことができる

1. 定義時以外値を追加できないので配列に比べて安全

実行時にenum定数を増やす
メソッド内で新しいenum定数を定義する
条件によってenumの種類を変える
→全部不可

2. 定義外の値の参照をできないので配列に比べて安全

enum Color { RED, GREEN, BLUE }
String[] colors = {"RED", "GREEN", "BLUE"};
    
Color c1 = Color.PURPLE; //  コンパイルエラー
String c2 =  "PURPLE"; //コンパイルは通るが、目的外の定義

となり、コンパイル時点でミスに気付けるため、規模の大きいコード程恩恵があると感じました。

3. 状態の定義に便利

enumの場合

enum Color { RED, GREEN, BLUE }
Color c = Color.PURPLE; //  同様にコンパイルエラー
switch (c) {
    case RED:
        break;
    case GREEN:
        break;
}

配列の場合

String[] colors = {"RED", "GREEN", "BLUE"};

String c =  "READ";
switch (c) {
    case RED:
        break;
    case GREEN:
        break;
}
//コンパイルは通る
//挙動はおそらく目的外

2と同様に、列挙型の場合、コンパイル時点でミスに気づけます。

4. 複数の値、複数の型を保持でき、渡すことができる

enum_test2
public class enum_test2{ 

    enum Status {
    //データ列挙ブロック------------------//
        SUCCESS(200, "成功", true),
        ERROR(500, "失敗", false);
    //----------------------------------//


    //フィールドの名前と型の宣言ブロック---//
        int code;
        String message;
        boolean tof;
    //----------------------------------//


    //Statusのコンストラクタ-------------//
        Status(int code, String message, boolean tof) {
            this.code = code;
            this.message = message;
            this.tof = tof;
        }
    //----------------------------------//
    }

    public static void main(String args[]){
        System.out.println( Status.SUCCESS.code );     // 200
        System.out.println( Status.SUCCESS.message );  // "成功"
        System.out.println( Status.SUCCESS.tof );      // true
        System.out.println( Status.ERROR.code );       // 500
    }
}

このように複数の値、型の情報を安全に保持できます。

おまけ

emum_test3
public class enum_test3{ 

enum Status {
//データ列挙ブロック------------------//
    SUCCESS(200, "成功", true),
    ERROR(500, "失敗", false);
//----------------------------------//


//フィールドの名前と型の宣言ブロック---//
    private final int code;
    private final String message;
    private final boolean tof;
//----------------------------------//


//Statusのコンストラクタ-------------//
    Status(int code, String message, boolean tof) {
        this.code = code;
        this.message = message;
        this.tof = tof;
    }
//----------------------------------//


//Statusのメソッド------------------//

    int getCode() {
        return code;
    }

    String getMessage() {
        return message;
    }

    boolean isTof() {
        return tof;
    }
    
//----------------------------------//

}

public static void main(String args[]){
    System.out.println( Status.SUCCESS.getCode );     // 200
    System.out.println( Status.SUCCESS.getMessage );  // "成功"
    System.out.println( Status.SUCCESS.isTof );      // true
    System.out.println( Status.ERROR.getCode );       // 500
}
}

値の参照をgetCode(),getMessage()メソッドで取得できるようにしました。
コンストラクタだけでなくメソッドも持つことができるので、実態としてはほぼクラスのような振る舞いをすると解釈をしています。(学習が進み、解釈の間違いに気づいたら追記します。)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?