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)
上一篇 2026年4月18日 上午9:01
下一篇 2026年4月18日 上午9:07


相关推荐

  • python读取txt文件中的json数据

    python读取txt文件中的json数据txt文本文件能存储各式各样数据,结构化的二维表、半结构化的json,非结构化的纯文本。存储在excel、csv文件中的二维表,都是可以直接存储在txt文件中的。半结构化的json也可以存储在txt文本文件中。最常见的是txt文件中存储一群非结构化的数据:今天只学习:从txt中读出json类型的半结构化数据importpandasaspdimportjsonf=o…

    2022年5月18日
    155
  • 使用自己的x3数据集实现SRGAN,小白进坑全收录!

    使用自己的x3数据集实现SRGAN,小白进坑全收录!本文所使用的代码为 https github com open mmlab mmsr 所使用的环境为 python3 6 8 CUDA9 0 176 cuDNN7 6 1 查看 cuda 和 cuDNN 版本的代码如下 查看 cuda 版本 cat usr local cuda version txt 查看 cuDNN 版本 cat usr local cuda include cudn

    2026年3月17日
    2
  • SQL日期时间格式转换大全

    SQL日期时间格式转换大全sqlserver200 中使用 convert 来取得 datetime 数据类型样式 全 日期数据格式的处理 两个示例 CONVERT varchar 16 时间一 20 结果 2007 02 0108 02 时间一般为 getdate 函数或数据表里的字段 CONVERT varchar 10 时间一 23 结果 2007 02 01 varchar 10 表

    2026年3月26日
    3
  • 软件测试理论试题及答案

    软件测试理论试题及答案一 选择题 一分一题共 45 分 下列软件属性中 7 软件产品首要满足的应该是 A A 功能需求 B 性能需求 C 可扩展性和灵活性 D 容错纠错能力软件缺陷产生的原因是 D A 交流不充分及沟通不畅 软件需求的变更 软件开发工具的缺陷 B 软件的复杂性 软件项目的时间压力 C 程序开发人员的错误 软件项目文档的缺乏 D 以上都是导致软件缺陷的最大原因是

    2026年3月19日
    2
  • 11.2 模型finetune

    11.2 模型finetune一 TransformLea 与 ModelFinetun 二 pytorch 中的 Finetune 一 TransferLear 与 ModelFinetun 什么是 TransferLear 迁移学习是机器学习的一个分支 主要研究源域的知识如何应用到目标域当中 迁移学习是一个很大的概念 怎么理解源域的知识应用到目标域当中呢 上图是来自一篇迁移学习的综述 左边是传统机器学习的过程 对于不同的任务分别学习得到不同的模型 而右边是迁移学习的示意

    2026年3月16日
    2
  • OpenClaw 本地部署 + 飞书接入全系统指南

    OpenClaw 本地部署 + 飞书接入全系统指南

    2026年3月12日
    2

发表回复

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

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