Java switch statement: Constant expression required, but it IS constant

前端 未结 13 1672
予麋鹿
予麋鹿 2020-11-22 10:42

So, I am working on this class that has a few static constants:

public abstract class Foo {
    ...
    public static final int BAR;
    public static final          


        
13条回答
  •  不要未来只要你来
    2020-11-22 11:18

    If you're using it in a switch case then you need to get the type of the enum even before you plug that value in the switch. For instance :

    SomeEnum someEnum = SomeEnum.values()[1];

    switch (someEnum) {
                case GRAPES:
                case BANANA: ...
    

    And the enum is like:

    public enum SomeEnum {
    
        GRAPES("Grapes", 0),
        BANANA("Banana", 1),
    
        private String typeName;
        private int typeId;
    
        SomeEnum(String typeName, int typeId){
            this.typeName = typeName;
            this.typeId = typeId;
        }
    }
    

提交回复
热议问题