You definitely can switch on enums.
Switch on Enum Using Traditional Switch and Case in Java We get the value from the enum using the constant's name like Days. MONDAY will fetch the constant MONDAY , and it will be stored in the enum object day . We can use it to switch between cases. switch() takes in the value to switch, that is day .
Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
Java Enum clone() Method The clone() method of Enum class throws CloneNotSupportedException. This method ensures that enums cannot be cloned, which helps to maintain their "singleton" property.
You definitely can switch on enums. An example posted from the Java tutorials.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
}
You actually can switch
on enum
s, but you can't switch
on String
s until Java 7. You might consider using polymorphic method dispatch with Java enum
s rather than an explicit switch
. Note that enum
s are objects in Java, not just symbols for int
s like they are in C/C++. You can have a method on an enum
type, then instead of writing a switch
, just call the method - one line of code: done!
enum MyEnum {
SOME_ENUM_CONSTANT {
@Override
public void method() {
System.out.println("first enum constant behavior!");
}
},
ANOTHER_ENUM_CONSTANT {
@Override
public void method() {
System.out.println("second enum constant behavior!");
}
}; // note the semi-colon after the final constant, not just a comma!
public abstract void method(); // could also be in an interface that MyEnum implements
}
void aMethodSomewhere(final MyEnum e) {
doSomeStuff();
e.method(); // here is where the switch would be, now it's one line of code!
doSomeOtherStuff();
}
First, you can switch on an enum
in Java. I'm guessing you intended to say you can’t, but you can. char
s have a set range of values, so it's easy to compare. Strings can be anything.
A switch
statement is usually implemented as a jump table (branch table) in the underlying compilation, which is only possible with a finite set of values. C# can switch on strings, but it causes a performance decrease because a jump table cannot be used.
Java 7 and later supports String
switches with the same characteristics.
Article on Programming.Guide: Switch on enum
enum MyEnum { CONST_ONE, CONST_TWO }
class Test {
public static void main(String[] args) {
MyEnum e = MyEnum.CONST_ONE;
switch (e) {
case CONST_ONE: System.out.println(1); break;
case CONST_TWO: System.out.println(2); break;
}
}
}
Switches for strings are implemented in Java 7.
You might be using the enums incorrectly in the switch cases. In comparison with the above example by CoolBeans.. you might be doing the following:
switch(day) {
case Day.MONDAY:
// Something..
break;
case Day.FRIDAY:
// Something friday
break;
}
Make sure that you use the actual enum values instead of EnumType.EnumValue
Eclipse points out this mistake though..
Actually you can use a switch statement with Strings in Java...unfortunately this is a new feature of Java 7, and most people are not using Java 7 yet because it's so new.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With