最终公共static int不能在switch语句中使用吗? [英] final public static ints can't be used in a switch statement?

查看:188
本文介绍了最终公共static int不能在switch语句中使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.以下代码有错误("..."代表省略的代码):

I'm confused. The following code has errors ("..." represents elided code):

int byteOrder = ...;
switch (byteOrder)
{
    case HDF5Constants.H5T_ORDER_BE:
        return ByteOrder.BIG_ENDIAN;
    ...
}

该错误在case语句上,并且Eclipse抱怨"case表达式必须是常量表达式".我在源文件中查找了此文件,并且它有一长串这样的行:

The error is on the case statement and Eclipse complains "case expressions must be constant expressions". 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 );

我认为您可以在开关语句中使用final public static int常量作为案例.我错了吗?

I thought you could use final public static int constants as cases in a switch statement. Am I wrong???

推荐答案

根据您显示的内容,H5T_ORDER_BE不是编译时常量(必须为编译时常量),它是在初始化时在运行时评估的班级.如果它求值为诸如123之类的常量(而不是看似是静态方法调用的常量),则编译器将不会抱怨.

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. 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.

这篇关于最终公共static int不能在switch语句中使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆