所以,我正在处理这个类,它有几个静态常数:

public abstract class Foo {
    ...
    public static final int BAR;
    public static final int BAZ;
    public static final int BAM;
    ...
}

然后,我想要一种方法来获得一个基于常量的相关字符串:

public static String lookup(int constant) {
    switch (constant) {
        case Foo.BAR: return "bar";
        case Foo.BAZ: return "baz";
        case Foo.BAM: return "bam";
        default: return "unknown";
    }
}

然而,当我编译时,我在3个case标签上都得到了一个常量表达式所需的错误。

我知道编译器需要表达式在编译时编译开关,但为什么不是Foo。BA_常数?


当前回答

在Android中做这样的事情时出现了这个错误:

 roleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (parent.getItemAtPosition(position)) {
                case ADMIN_CONSTANT: //Threw the error

            }

尽管声明了一个常数:

public static final String ADMIN_CONSTANT= "Admin";

我通过修改我的代码来解决这个问题:

roleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String selectedItem = String.valueOf(parent.getItemAtPosition(position));
            switch (selectedItem) {
                case ADMIN_CONSTANT:

            }

其他回答

如果你在一个开关的情况下使用它,那么你需要得到enum的类型,甚至在你插入该值在开关。例如:

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

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

枚举是这样的:

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;
    }
}

有时switch变量也会犯这样的错误,例如:

switch(view.getTag()) {//which is an Object type

   case 0://will give compiler error that says Constant expression required

   //...
}

要解决这个问题,你应该将变量转换为int(在这种情况下)。所以:

switch((int)view.getTag()) {//will be int

   case 0: //No Error

   //...
}

问题是整型和整数(我不知道为什么),改变整型字符串和它的工作

public abstract class Foo {
    ...
    public static final String BAR;
    public static final String BAZ;
    public static final String BAM;
    ...
}
public static String lookup(String constant) {
    switch (constant) {
        case Foo.BAR: return "bar";
        case Foo.BAZ: return "baz";
        case Foo.BAM: return "bam";
        default: return "unknown";
    }
}

这个问题很久以前就回答过了,可能不相关,只是以防万一。 当我遇到这个问题时,我简单地使用了if语句而不是switch,它解决了错误。 这当然是一种变通方法,可能不是“正确”的解决方案,但对我来说,这就足够了。

编辑:2021.01.21

这个答案有点误导,我想澄清一下。

用if语句替换switch语句不应该被认为是一个好的解决方案,在软件开发中存在switch和if这两个概念,以及在两者之间进行选择时考虑性能是很重要的,这是有很好的理由的。 虽然我提供了一个解决方案,提出的错误,我的回答没有阐明“为什么”问题发生,而是提供了一种解决问题的方法。

在我的特定情况下,使用if语句就足以解决问题。开发人员应该花点时间来决定这是否是解决当前问题的正确解决方案。

因此,这个答案应该仅仅被认为是我在第一个回答中所述的特定情况下的一个变通办法,而绝不是这个问题的正确答案

我建议使用以下方法:

public enum Animal {
    DOG("dog"), TIGER("tiger"), LION("lion");
    private final String name;

    Animal(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return this.name;
    }
}


public class DemoSwitchUsage {

     private String getAnimal(String name) {
         Animal animalName = Animal.valueOf(name);
         switch(animalName) {
         case DOG:
             // write the code required.
             break;
         case LION:
             // Write the code required.
             break;
         default:
             break;
         }
     }
}