[Java] Branch enum with switch statement

Introduction

Note that I was addicted to branching enum type variables with a switch statement.

What I tried to do

There was an enum like this and I tried to branch the passed enum type value with a switch statement.

HogeType.java


public enum HogeType {
  NONE(0),
  FIRST(1),
  SECOND(2)
}

Bad example

public void hoge(HogeType hogeType){
  switch(hogeType){
    case HogeType.FIRST:
      System.out.println("first");
      break;
    case HogeType.SECOND:
      System.out.println("second");
      break;
    default:
      System.out.println("none");
  }
}

There was an error in the case HogeType.FIRST: part of this. The content is ʻan enum switch case label must be the unqualified name of an enumeration constant`.

Correct answer

public void hoge(HogeType hogeType){
  switch(hogeType){
    case FIRST:
      System.out.println("first");
      break;
    case SECOND:
      System.out.println("second");
      break;
    default:
      System.out.println("none");
  }
}

According to the investigation, when enum type is taken as an argument of switch statement, it seems that the value given to case must be a constant. In other words, the description HogeType. is no longer necessary.

Recommended Posts

[Java] Branch enum with switch statement
Java switch statement
Switch java with direnv
Seasonal display with Java switch
switch statement
[Java] Really scary switch statement story
[Java] enum
[Java] Reduce if statements with Enum
Make something like Java Enum with Typescript
Java, if statement / switch statement starting from beginner
Basics of java basics ② ~ if statement and switch statement ~
Java conditional branch
java conditional branch
Java for statement
[Java] Conditional branch
Java switch statement and break, Kotlin when expression ...
Easily switch Java versions with alias on macOS
Switch versions with alternatives after yum install java
[Java] for statement, while statement
Java Enum utilization example
Java instruction execution statement
Change seats with java
Install Java with Ansible
[Java] for statement / extended for statement
Comfortable download with JAVA
[Java basics] Let's make a triangle with a for statement
Enum reverse map Java
Quickly implement a singleton with an enum in Java
Switch statement range specification
(Memo) Java for statement
Download Java with Ansible
[Java] Branch and repeat
Let's scrape with Java! !!
[Java] About enum type
Build Java with Wercker
Studying Java-Part 11-switch statement
Endian conversion with JAVA
Store in Java 2D map and turn with for statement
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
[Swift] switch statement using tuples
Java to play with Function
Try DB connection with Java
[Java] Basic statement for beginners
Swift conditional branch statement summary
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Try gRPC with Java, Maven
Let's operate Excel with Java! !!
Version control Java with SDKMAN
Java if and switch statements