[英]final public static ints can't be used in a switch statement?
I'm confused. 我糊涂了。 The following code has errors ("..." represents elided code): 以下代码有错误(“...”代表省略代码):
int byteOrder = ...;
switch (byteOrder)
{
case HDF5Constants.H5T_ORDER_BE:
return ByteOrder.BIG_ENDIAN;
...
}
The error is on the case statement and Eclipse complains "case expressions must be constant expressions". 错误在case语句上,Eclipse抱怨“case表达式必须是常量表达式”。 I looked in the source file for this and it has a long list of lines like this: 我在源文件中查找了这个,它有很长的列表,如下所示:
final public static int H5T_ORDER_BE = H5.J2C( JH5T_ORDER_BE );
I thought you could use final public static int
constants as cases in a switch statement. 我认为你可以使用final public static int
constants作为switch语句中的case。 Am I wrong??? 我错了吗???
From what you've shown H5T_ORDER_BE
is not a compile-time constant (which it needs to be) - it's evaluated at runtime during the initialisation of the class. 根据您的显示, H5T_ORDER_BE
不是编译时常量(它需要) - 它在类的初始化期间在运行时进行评估。 If it evaluated to a constant such as 123
(rather than what appears to be a static method call) then the compiler wouldn't complain. 如果它计算为常量(如123
(而不是看起来像是静态方法调用),那么编译器就不会抱怨。
You are wrong! 你错了! :-) :-)
Case statements can refer only to constants. case语句只能引用常量。 A static variable is initialized at runtime, so it can't be used here. 静态变量在运行时初始化,因此不能在此处使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.