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


相关推荐

  • sha1给出了三种新的sha版本_开发版和稳定版哪个好

    sha1给出了三种新的sha版本_开发版和稳定版哪个好在进行百度地图等地图开发时,需要申请一个SHA1,方法流程如下:1.找到文件debug.keystore所在位置,一般在C盘的.android目录下。2.使用jdk自带的keytool工具。3.进入win+r输入cmd进入cmd窗口,进入debug.keystore所在目录,使用keytool工具,示例:C:\Users\SYY\.android>D:\SYYData\.download\IDEA-C\jdks\bin\keytool-list-v-keystoredebug.keys

    2022年8月11日
    7
  • [TCP/IP] 基础知识总结

    [TCP/IP] 基础知识总结课堂笔记整理。IPMACARPICMP你需要掌握那些知识?

    2022年6月29日
    24
  • PyTorch 中的数据类型 torch.utils.data.DataLoader

    PyTorch 中的数据类型 torch.utils.data.DataLoaderDataLoader是PyTorch中的一种数据类型。在PyTorch中训练模型经常要使用它,那么该数据结构长什么样子,如何生成这样的数据类型?下面就研究一下:先看看 dataloader.py脚本是怎么写的(VS中按F12跳转到该脚本) __init__(构造函数)中的几个重要的属性:1、dataset:(数据类型dataset)输入的数据类型。看名字感觉就像是数据库,…

    2022年5月20日
    92
  • linux修改java环境,linux下修改java环境变量

    linux修改java环境,linux下修改java环境变量linux下修改java环境变量[2021-02-0701:55:24]简介:linux修改php环境变量的方法:首先添加路径,代码为【exportPATH=”$PATH:/cxwww/server/php/7.3.4/bin”】;然后读取配置即可,代码为【source/etc/profile】。linux修改php环境变服务器这篇文章主要为大家展示了linux环境下怎么配置环境变量,内…

    2025年6月19日
    2
  • javascript获取当前系统时间代码_获取当前系统时间

    javascript获取当前系统时间代码_获取当前系统时间JavaScript获取当前时间time开发常用时间笔记JS获取当前时间Js获取当前日期时间及其它操作**谨记要懂得经常在控制台输出结果**varmyDate=newDate();myDate.getYear();//获取当前年份(2位)myDate.getFullYear();//获取完整的年份(4位,1970-???)myDate.getMonth();//获取当前月份(0-11,0代表1月)myDate.getDate();

    2022年9月23日
    0
  • html制作图片幻灯片效果代码,【JS+CSS3】实现带预览图幻灯片效果的示例代码

    html制作图片幻灯片效果代码,【JS+CSS3】实现带预览图幻灯片效果的示例代码一、前期准备1.1案例分析适用场景:单例布局1.2方法论V视图HTML+CSS+调试Cjs实现控制流程D数据优化扩展二、代码结构h2captionh3caption样式(CSS略)脚本功能开发>>内容输出Template改造输出幻灯片&控制按钮图片位置调整>>切换控制切换幻灯片.main_i_active切换控制按钮.ctrl…

    2022年7月13日
    17

发表回复

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

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