0
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?

More than 1 year has passed since last update.

【 Java 】Enumクラスについて

Last updated at Posted at 2023-03-07

■Enumとは?

Enumとは列挙型と言われていて、複数の定数を効率的に管理できるクラスである。

Enumのメリット、デメリットの理解はまた今度。。。

■構文

Enum構文
アクセス修飾子 enum 列挙型名 { 列挙子1, 列挙子2, 列挙子3 ... }

■使い方

サンプルコード
/**
 * 口座種類を列挙型で定義
 */
public enum Kouza {
    	HUTU, // 普通口座
		TOZA, // 当座口座
		TEKI, // 定期口座
	}

/**
 * 口座種類に応じて処理を変えるメソッド
 * @param kouza 口座
 */
public static void setAccountType(Kouza kouza) {
		switch(kouza) {
		case HUTU:
			System.out.println("普通口座");
			break;
		
		case TOZA:
			System.out.println("当座口座");
			break;
		
		case TEKI:
			System.out.println("定期口座");
			break;
	}
}

public static void main(String[] args) {
		// ①口座種類全て出力
		for (Kouza kouza : Kouza.values()) {
			System.out.println(kouza.ordinal() + ":" + kouza);
		}

        // ②特定の口座種類を出力
		setAccountType(Kouza.HUTU);
}

上記ソースの場合、コンソールには以下内容が出力される。

実行結果
// ①の出力結果
    0:HUTU
    1:TOZA
    2:TEKI

// ②
    普通口座
0
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
0
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?