代码之家  ›  专栏  ›  技术社区  ›  Austin Hyde

Java开关语句:需要常量表达式,但它是常数

  •  143
  • Austin Hyde  · 技术社区  · 15 年前

    所以,我正在处理这个类,它有一些静态常量:

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

    但是,当我编译时,我得到一个 constant expression required 3个案例标签中的每一个都有错误。

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

    10 回复  |  直到 7 年前
        2
  •  69
  •   Tony Ennis    15 年前

    public abstract class Foo {
        ...
        public static final int BAR=0;
        public static final int BAZ=1;
        public static final int BAM=2;
        ...
    }
    
        3
  •  40
  •   Teo Inke    11 年前

    public static final int TAKE_PICTURE = 1;
    

    public static int TAKE_PICTURE = 1;
    
        4
  •  28
  •   Sheldon L. Cooper    15 年前

    public static final int BAR = new Random().nextInt();
    

    BAR

        5
  •  17
  •   Cosmin    14 年前

    public class MainClass {
    enum Choice { Choice1, Choice2, Choice3 }
    public static void main(String[] args) {
    Choice ch = Choice.Choice1;
    
    switch(ch) {
      case Choice1:
        System.out.println("Choice1 selected");
        break;
     case Choice2:
       System.out.println("Choice2 selected");
       break;
     case Choice3:
       System.out.println("Choice3 selected");
       break;
        }
      }
    }
    

    Switch statement with enum

        6
  •  0
  •   Mahdi-Malv    8 年前

    switch(view.getTag()) {//which is an Object type
    
       case 0://will give compiler error that says Constant expression required
    
       //...
    }
    

    switch((int)view.getTag()) {//will be int
    
       case 0: //No Error
    
       //...
    }
    
        7
  •  0
  •   Ojonugwa Jude Ochalifu Dave    7 年前

     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:
    
                }
    
        8
  •  0
  •   Janaco    7 年前

    switch (tipoWebServ) {
                                case VariablesKmDialog.OBTENER_KM:
                                    resultObtenerKm(result);
                                    break;
                                case var.MODIFICAR_KM:
                                    resultModificarKm(result);
                                    break;
                            }
    

    var.MODIFICAR_KM: VariablesKmDialog.OBTENER_KM

        9
  •  0
  •   Samer Murad    7 年前

    if switch

        10
  •  -1
  •   everton Raghunandan Krishnamurthy    14 年前

    public enum Foo 
    {
        BAR("bar"),
        BAZ("baz"),
        BAM("bam");
    
        private final String description;
    
        private Foo(String description)
        {
            this.description = description;
        }
    
        public String getDescription()
        {
            return description;
        }
    }
    

    System.out.println(Foo.BAR.getDescription());