为什么要使用“final static int”?可以用作开关的大小写常量,但不能用作“final static”
为什么这个 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;
}
}
}
虽然这个 enum 开关不是:
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;
}
}
}
我知道在这种情况下必须是一个常量,那么为什么我可以使用“final static int”作为常量而不是“final static
Why is this int switch valid:
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>"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为 case 语句标签必须具有编译时常量或 EnumConstantName。 JLS 14.11
编译时间常数只能是字符串和原始类型,如
Because a case statement label must have either a compile time constant or an 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.
case 参数必须是原始的;它不能是一个对象。
但是,您可以按如下方式使用枚举:
由于
value
被声明为RetentionPolicy
类型,因此您可以直接在 switch 内部使用枚举常量。The case argument must be primitive; it cannot be an object.
However, you can use enums as follows:
Because
value
is declared to be of typeRetentionPolicy
you can use the enum constants directly inside the switch.或者简单地使用 if-elseif 情况:
Or simply use a if-elseif case :
编译器会说,
因此您的
RT
值需要为RUNTIME
而不是RetentionPolicy.RUNTIME
才能使您的代码正常工作。但这当然是不可能的。为什么不直接使用RetentionPolicy
枚举?如果您想坚持最终静态声明,则需要将整个枚举分配给最终静态变量。The compiler says
So your value of
RT
would need to beRUNTIME
instead ofRetentionPolicy.RUNTIME
to make your code work. But of course that is not possible. Why not use theRetentionPolicy
enum directly? If you want to stick to your final static declaration, you need to assign the whole enum to your final static variable.我有类似的要求,并通过打开枚举序数而不是打开枚举本身来解决这个问题。这不是很漂亮/直观,但它有效:
请注意,当然不可能将常量声明为例如,
出于同样的原因,人们无法首先将常量声明为枚举......
I had a similar requirement and worked around this problem by switching on the Enums ordinal number instead of switching on the enum itself. This is not very beautiful/intuitive but it works:
Note that it is of course not possible to declare the constant as e.g.
for the same reason one is not able to declare the constant as an Enum in the first place...