【问题标题】:constant expression required while using constants使用常量时需要常量表达式
【发布时间】:2017-03-06 15:38:34
【问题描述】:

private static int counter = 0;
public static final int CLIENT_REQUEST_TIME = 计数器++;

...

switch(命令){
case Command.CLIENT_REQUEST_TIME:

...

但它来了

"错误:(20, 25) java: 需要常量表达式",

案例陈述。

但究竟为什么呢?因为 CLIENT_REQUEST_TIME 是常数

我知道这应该是针对 Oracle 的,我可以从数百万种解决方法中进行选择...但以防万一有人能看到背后的一些逻辑,它肯定会让我今晚睡得更好:)

【问题讨论】:

    标签: switch-statement case constants final


    【解决方案1】:

    出现此错误是因为您将非常量属性设置为常量字段。我不明白这段代码必须做什么,但也许你可以把它改成这样:

    private static int counter = 0; public static int CLIENT_REQUEST_TIME = counter++;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2022-11-15
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多