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

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

    Below code is self-explanatory, We can use an enum with a switch case:

    /**
     *
     */
    enum ClassNames {
        STRING(String.class, String.class.getSimpleName()),
        BOOLEAN(Boolean.class, Boolean.class.getSimpleName()),
        INTEGER(Integer.class, Integer.class.getSimpleName()),
        LONG(Long.class, Long.class.getSimpleName());
        private Class typeName;
        private String simpleName;
        ClassNames(Class typeName, String simpleName){
            this.typeName = typeName;
            this.simpleName = simpleName;
        }
    }
    

    Based on the class values from the enum can be mapped:

     switch (ClassNames.valueOf(clazz.getSimpleName())) {
            case STRING:
                String castValue = (String) keyValue;
                break;
            case BOOLEAN:
                break;
            case Integer:
                break;
            case LONG:
                break;
            default:
                isValid = false;
    
        }
    

    Hope it helps :)

    0 讨论(0)
  • 2020-11-22 11:05

    I recommend you to use enums :)

    Check this out:

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

    Then you can use it like this:

    System.out.println(Foo.BAR.getDescription());
    
    0 讨论(0)
  • 2020-11-22 11:10

    Because those are not compile time constants. Consider the following valid code:

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

    You can only know the value of BAR in runtime.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 11:10

    I recommend using the following way:

    public enum Animal {
        DOG("dog"), TIGER("tiger"), LION("lion");
        private final String 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;
             }
         }
    }
    
    0 讨论(0)
  • 2020-11-22 11:11

    Sometimes the switch variable can also make that error for example:

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

    To solve you should cast the variable to int(in this case). So:

    switch((int)view.getTag()) {//will be int
    
       case 0: //No Error
    
       //...
    }
    
    0 讨论(0)
提交回复
热议问题