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

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


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


相关推荐

  • 查看linux执行的命令记录_linuxhistory命令

    查看linux执行的命令记录_linuxhistory命令前言我们每次敲打linux命令的时候,有时候想用之前用过的命令,一般情况下,我们都会按↑↓箭头来寻找历史的命令记录,那如果我想用1天前执行的某条命令,难道还要按↑100次?显示这样是不现实的,我们可

    2022年7月28日
  • ASP.NET中水晶报表的使用

    ASP.NET中水晶报表的使用作者:caoli                             在我们对VS.Net中的水晶报表(CrystalReports)进行研究之前,我和我朋友对如何将这个复杂的东东加入我们的Web应用有着非常的好奇心。一周以后,在阅读了大量的“HOWTO”文档之后,我们成功地将一些简单的报告加入到了我们的Asp.net程序中,并得到了一些小决窍。  这篇文章教你如何在.NetWe…

    2022年5月20日
  • leetcode 回文数_字符串转换为整数

    leetcode 回文数_字符串转换为整数原题链接请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。函数 myAtoi(string s) 的算法如下:读入字符串并丢弃无用的前导空格检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。将前面步骤读入的这些数字转换为整数(即,“1

    2022年8月9日
  • Vue安装及环境配置、开发工具

    Vue安装及环境配置、开发工具vue安装环境搭建提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录vue安装环境搭建前言一、node.js安装和配置1.下载安装node.js2.配置默认安装目录和缓存日志目录3.node.js环境配置4.配置淘宝镜像源二、使用步骤1.引入库2.读入数据总结前言vue前端框架的环境搭建一、node.js安装和配置1.下载安装node.js官网下载最新版本:https://nodejs.org/en/download/可以下载安装包(安装教程见:http

    2022年6月11日
  • 生成h5文件_h5实现文件下载

    生成h5文件_h5实现文件下载生成训练h5文件importh5pyimportosimportcv2importmathimportnumpyasnpimportrandomimportroot_path=”/home/tyd/caffe_case/HDF5/image”withopen(“/home/tyd/caffe_case/HDF5/hdf5.txt”,”r”)…

    2022年9月9日
  • vs2008sp1补丁怎么安装_win10系统怎么安装cad2005

    vs2008sp1补丁怎么安装_win10系统怎么安装cad2005我第一次安装这个补丁的时候就挂了,搞得VS2005和相关的一些程序都不能运行,后来在网上找到了一些解决方法,结合自己的实际体验,写了这篇教程。补丁相关资料:简体中文版补丁名称:VS80sp1-KB926604-X86-CHS.exe版本:50727.762知识库(KB)文章:KB928957发布日期:2006/12/14简体中文版补丁大小:430

    2022年9月28日

发表回复

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

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