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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 共享打印机错误代码709_连接共享打印机错误0*0000011b

    共享打印机错误代码709_连接共享打印机错误0*0000011b最近发现很多用户连接或安装局域网共享的打印机时出现很多问题,常见的错误代码是0x0000011b和0x00000709或0x000006d9这三个错误。要如何解决呢?下面来讲一下如何解决这两个问题。键盘组合键徽标键Win+R键打开运行,在弹出的运行框中输入【services.msc】确定打开服务窗口,检查这两个服务是否已启动:PrintSpooler和WindowsFirewall一般Win7易出的错误6d9是后面的服务未启动所致。依次查找并卸载KB5005565、KB5005566、KB5005…

    2025年10月20日
    5
  • MySQL的安装和配置(超详细图文教程)「建议收藏」

    MySQL的安装和配置(超详细图文教程)「建议收藏」数据库的安装1.打开下载的mysql安装文件双击解压缩,运行“mysql-5.5.40-win32.msi”。2.选择安装类型,有“Typical(默认)”、“Complete(完全)”、“Custom(用户自定义)”三个选项,选择“Custom”,按“next”键继续。3.点选“Browse”,手动指定安装目录。4.填上安装目录,我的是“d:\ProgramFiles(x86)…

    2022年4月20日
    53
  • 电子设计(2)三极管稳压电路仿真分析「建议收藏」

    电子设计(2)三极管稳压电路仿真分析「建议收藏」这篇博客主要是三极管稳压电路的仿真分析。

    2022年6月20日
    34
  • SilverLight浏览器支持情况

    SilverLight浏览器支持情况推荐使用IE、360浏览器,搜狗、QQ浏览器需切换到兼容模式。以下浏览器版本不再支持:Chrome45及以后版本、Firefox52及以后版本、MicrosoftEdge浏览器、Opera浏览器。…

    2022年7月17日
    33
  • Python的正则表达式_python正则表达式例子

    Python的正则表达式_python正则表达式例子nlp任务中,正则表达式是一个很好的工具。推荐资源:https://github.com/ziishaned/learn-regex/https://regex101.com/在线练习结合网上的教程,我的整理如下:先来举个例子:#解析网页HTML<html><body><h1>helloworld&…

    2025年12月6日
    3
  • C#面试题集_c++面试题库

    C#面试题集_c++面试题库//题目:找出字符串中出现次数最多的字符及出现次数publicstaticvoidFun(){stringstr=”iamamandag!”;Dictionaryd=newDictionary();for(inti=0;i

    2022年8月29日
    4

发表回复

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

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