Why do I get an Enum constant reference cannot be qualified in a case label?

后端 未结 3 1619
你的背包
你的背包 2020-12-08 06:42

Why does the following code fail to compile, while changing the case statement to

case ENUM1: doSomeStuff();

works?

public          


        
3条回答
  •  感情败类
    2020-12-08 07:13

    This is not really answering your question but if you have code depending on the enum value, you can also create an abstract method in your enum that gets overloaded for every value:

    public enum EnumType {
        ENUM1 {
            @Override
            public void doSomeStuff() {
                // do something
            }
        },
        ENUM2 {
            @Override
            public void doSomeStuff() {
                // do something else
            }
        };
    
        public abstract void doSomeStuff();
    }
    

提交回复
热议问题