gpt4 book ai didi

java - 最终变量对于 switch 情况来说不够 const

转载 作者:行者123 更新时间:2023-12-01 18:16:30 25 4
gpt4 key购买 nike

我有以下代码:

public class Main {

static final String fu = Week.class.getName();

public static void main(String[] arg0){

String h = "dummy";
switch (h) {
* case fu:
System.out.println(8);
break;
default:
break;
}
}
}

现在 Eclipse 提示 * case 表达式必须是常量表达式。但我让 fu 尽可能保持不变!为什么这还不够,我能做什么(除了 if-else 之外)?

最佳答案

Why is this not enough

因为根据 JLS 15.28 的规则,它不是编译时常量.

Why is this not enough, what can I do (other than if-else)?

基本上什么都没有 - 除非你使用字符串文字,这显然不是很令人愉快。

关于java - 最终变量对于 switch 情况来说不够 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com