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

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 int BAZ;     public static final int BAM;     ... }

Then, I would like a way to get a relevant string based on the constant:

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

However, when I compile, I get a constant expression required error on each of the 3 case labels.

I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn't Foo.BA_ constant?

回答1:

I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn't Foo.BA_ constant?

While they are constant from the perspective of any code that executes after the fields have been initialized, they are not a compile time constant in the sense required by the JLS; see §15.28 Constant Expressions for a definition of what is required of a constant expression. This refers to §4.12.4 Final Variables which defines a "constant variable" as follows:

We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

In your example, the Foo.BA* variables do not have initializers, and hence do not qualify as "constant variables". The fix is simple; change the Foo.BA* variable declarations to have initializers that are compile-time constant expressions.



回答2:

You get Constant expression required because you left the values off your constants. Try:

public abstract class Foo {     ...     public static final int BAR=0;     public static final int BAZ=1;     public static final int BAM=2;     ... }


回答3:

I got this error on Android, and my solution was just to use:

public static final int TAKE_PICTURE = 1;

instead of

public static int TAKE_PICTURE = 1;


回答4:

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.



回答5:

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



回答6:

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());


回答7:

This was answered ages ago and probably not relevant, but just in case. When I was confronted with this issue, I simply used an if statement instead of switch, it solved the error



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!