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

前端 未结 13 1657
予麋鹿
予麋鹿 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:10

    You can use an enum like in this example:

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

    Source: Switch statement with enum

提交回复
热议问题