Note that I was addicted to branching enum type variables with a switch statement.
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)
}
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`.
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