java 为什么“final static int”可以用作开关的大小写常量而不是“final static <your enum>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4401743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why "final static int" can be used as a switch's case constant but not "final static <your enum>"
提问by pakman
Why is this int switch valid:
为什么这个 int 开关有效:
public class Foo {
private final static int ONE = 1;
private final static int TWO = 2;
public static void main(String[] args) {
int value = 1;
switch (value) {
case ONE: break;
case TWO: break;
}
}
}
While this enum switch is not:
虽然这个枚举开关不是:
import java.lang.annotation.RetentionPolicy;
public class Foo {
private final static RetentionPolicy RT = RetentionPolicy.RUNTIME;
private final static RetentionPolicy SRC = RetentionPolicy.SOURCE;
public static void main(String[] args) {
RetentionPolicy value = RetentionPolicy.RUNTIME;
switch (value) {
case RT: break;
case SRC: break;
}
}
}
I know that what goes in the case must be a constant, so why can I use a "final static int" as constant but not a "final static <your enum>"?
我知道在这种情况下发生的事情必须是一个常量,那么为什么我可以使用“final static int”作为常量而不是“final static <your enum>”?
采纳答案by ILMTitan
Because a case statement label must have either a compile time constant or an EnumConstantName. JLS 14.11
因为 case 语句标签必须具有编译时常量或 EnumConstantName。JLS 14.11
Compile time constants can only be strings and primitive types, as described by JLS 15.28. Thus you can not use a static final <your enum>, as it is neither a compile time constant, nor the name of an enum.
编译时常量只能是字符串和原始类型,如JLS 15.28 所述。因此,您不能使用静态 final <your enum>,因为它既不是编译时常量,也不是枚举的名称。
回答by Cameron Skinner
The case argument must be primitive; it cannot be an object.
case 参数必须是原始的;它不能是一个对象。
However, you can use enums as follows:
但是,您可以按如下方式使用枚举:
RetentionPolicy value = ...
switch (value) {
case RUNTIME:
case SOURCE:
}
Because value
is declared to be of type RetentionPolicy
you can use the enum constants directly inside the switch.
因为value
被声明为类型,所以RetentionPolicy
您可以直接在开关内使用枚举常量。
回答by Rollyng
Or simply use a if-elseif case :
或者简单地使用 if-elseif 案例:
private final static int ONE = 1;
private final static int TWO = 2;
public static void main(String[] args) {
int value = 1;
if(value.equals(ONE)){
}
else if(value.equals(ONE)){
}
}
回答by mdm
The compiler says
编译器说
unqualified enumeration constant name required
So your value of RT
would need to be RUNTIME
instead of RetentionPolicy.RUNTIME
to make your code work. But of course that is not possible. Why not use the RetentionPolicy
enum directly? If you want to stick to your final static declaration, you need to assign the whole enum to your final static variable.
所以你的价值RT
需要是RUNTIME
而不是RetentionPolicy.RUNTIME
让你的代码工作。但这当然是不可能的。为什么不RetentionPolicy
直接使用枚举?如果你想坚持你的最终静态声明,你需要将整个枚举分配给你的最终静态变量。
回答by Balder
I had a similar requirement and worked around this problem by switching on the Enums ordinal numberinstead of switching on the enum itself. This is not very beautiful/intuitive but it works:
我有一个类似的要求,并通过打开 Enums 序号而不是打开 enum 本身来解决这个问题。这不是很漂亮/直观,但它有效:
public class Foo {
private final static int SRC = 0; // == RetentionPolicy.SOURCE.ordinal();
private final static int RT = 2; // == RetentionPolicy.RUNTIME.ordinal();
static{
if (RT != RetentionPolicy.RUNTIME.ordinal() || SRC != RetentionPolicy.SOURCE.ordinal()) {
throw new IllegalStateException("Incompatible RetentionPolicy.class file");
}
}
public static void main(String[] args) {
RetentionPolicy value = RetentionPolicy.RUNTIME;
switch (value.ordinal()) {
case RT: break;
case SRC: break;
}
}
}
Note that it is of course not possible to declare the constant as e.g.
请注意,当然不可能将常量声明为例如
private final static int SRC = RetentionPolicy.SOURCE.ordinal();
for the same reason one is not able to declare the constant as an Enum in the first place...
出于同样的原因,首先无法将常量声明为 Enum ......