java 常量 表达式,Java switch语句:需要常量表达式,但它是常量

java 常量 表达式,Java switch语句:需要常量表达式,但它是常量So,Iamworkingonthisclassthathasafewstaticconstants:publicabstractclassFoo{…publicstaticfinalintBAR;publicstaticfinalintBAZ;publicstaticfinalintBAM;…}Then,Iwouldlike…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

java 常量 表达式,Java switch语句:需要常量表达式,但它是常量

So, I am working on this class that has a few static constants:

public abstract class Foo {

public static final int BAR;

public static final int BAZ;

public static final int BAM;

}

Then, I would like a way to get a relevant string based on the constant:

public static String lookup(int constant) {

switch (constant) {

case Foo.BAR: return “bar”;

case Foo.BAZ: return “baz”;

case Foo.BAM: return “bam”;

default: return “unknown”;

}

}

However, when I compile, I get a constant expression required error on each of the 3 case labels.

I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn’t Foo.BA_ constant?

解决方案

I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn’t Foo.BA_ constant?

While they are constant from the perspective of any code that executes after the fields have been initialized, they are not a compile time constant in the sense required by the JLS; see §15.28 Constant Expressions for a definition of what is required of a constant expression. This refers to §4.12.4 Final Variables which defines a “constant variable” as follows:

We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

In your example, the Foo.BA* variables do not have initializers, and hence do not qualify as “constant variables”. The fix is simple; change the Foo.BA* variable declarations to have initializers that are compile-time constant expressions.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187918.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android Studio(AS)–>导入项目

    Android Studio(AS)–>导入项目博文更新2015-10-22:AndroidStudio1.4版本开始,可以直接打开Eclipse项目;1:首先,你必须要有一个工程(Project),才可以打开项目(Module);(注意:Eclipse中的Workspace对应AndroidStudio中的Project,Eclipse中的Project对应AndroidStudio中的Module,);

    2022年5月10日
    45
  • 小程序获取openid40029[通俗易懂]

    小程序获取openid40029[通俗易懂]小程序获取openid40029{“errcode”:40029,“errmsg”:“invalidcode,hints:[req_id:XDgCIfyFe-.LPgiA]”}还出现了errcode:002的情况错误原因:小程序开发者工具中的appid和后台的appid不一致总结40029有可能是因为1.appid与密钥不一致(重置下密钥也可以)2.前后端的a…

    2022年4月28日
    44
  • 艺术的问题

    艺术的问题

    2021年12月31日
    49
  • CSDN 2018博客之星评选,感谢大家的投票

    那些曾走过的路,看过的风景!2018博客之星评选,我在CSDN三年了,虽然没成为大神,大佬,但是这一路上一直在坚持,在进步, 感谢大家一路对我的支持和鼓励,虽然2018年这一年过的比较压抑,但是我期待2019。2019我会好好调整自己,输出更多好的内容!谢谢你为我投票! 2019祝你我 有酒,有肉 ,有故事! 我的投票专属地址 : 点我投票 #70 ,谢谢感谢 2018 那些走…

    2022年2月27日
    36
  • matlab资产组合最优配置_最优投资组合怎么确定

    matlab资产组合最优配置_最优投资组合怎么确定您所在位置:网站首页>海量文档&nbsp>&nbsp计算机&nbsp>&nbspmatlab基于MATLAB的最优投资组合问题.pdf3页本文档一共被下载:次,您可全文免费在线阅读后下载本文档。下载提示1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。2.该文档所得收…

    2025年7月11日
    3
  • 通配符掩码计算「建议收藏」

    通配符掩码计算「建议收藏」一,通配符掩码1.通配符掩码的用途和结构①用途通配符掩码(wildcard-mask)路由器使用的通配符掩码与源或目标地址一起来分辨匹配的地址范围,它与子网掩码不同。它不像子网掩码告诉路由器IP地址的哪一位属于网络号一样,通配符掩码告诉路由器为了判断出匹配,它需要检查IP地址中的多少位。②结构通配符掩码中,0表示要检查的位,1表示不需要检查的位通配符掩码中,可以用255.255.255…

    2022年7月24日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号