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


相关推荐

  • php Restful设计

    php Restful设计

    2021年10月22日
  • java怎么测试_java中如何使用Junit测试[通俗易懂]

    java怎么测试_java中如何使用Junit测试[通俗易懂]java中如何使用Junit测试一、总结一句话总结:a、单元测试的测试代码在test文件夹下,和源码不在同一个文件夹下b、测试的类方法都以test开头,后面接要测试的类或者方法的名字1、JUnit中什么时候使用assertTrue,assertFalse语句?true通过false通过assertTrue(booleancondition);condition:如果condition结果为t…

    2022年7月8日
  • hash碰撞解决方法

    hash碰撞解决方法Hash碰撞冲突我们知道,对象Hash的前提是实现equals()和hashCode()两个方法,那么HashCode()的作用就是保证对象返回唯一hash值,但当两个对象计算值一样时,这就发生了碰撞冲突。如下将介绍如何处理冲突,当然其前提是一致性hash。1.开放地址法开放地执法有一个公式:Hi=(H(key)+di)MODmi=1,2,…,k(k<=m-1)其中,m为哈希表的表长。…

    2022年6月17日
  • vmware linux安装教程_vmware10虚拟机安装教程

    vmware linux安装教程_vmware10虚拟机安装教程一、安装VMware下载地址(16pro):https://www.aliyundrive.com/s/FSktJJXsfa8安装:选一下安装地址,一直下一步即可。(可能会要求重启电脑,重启即可)二、安装Linux下载地址:CentOS-7.5提取码:486k接下来看图操作2.1新建虚拟机现在我们就相当于买电脑,先把电脑配置整好。什么cpu啊内存条啊硬盘啊什么乱七八糟的,先不着急装系统。这里看你装什么版本的Linux了,我装的是GenOS7.564位所以选的是Ge

    2022年10月8日
  • 用html做简单的日记,学习HTML日记[通俗易懂]

    用html做简单的日记,学习HTML日记[通俗易懂]1。html>是什么意思?[1]DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档。html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明。表示网页采用html52.开始标签结束标签3.这是最外的一层中文编码目前在大部分浏览器中,…

    2022年5月27日
  • Linux系列——VMware虚拟机配置端口转发(端口映射),实现远程访问[通俗易懂]

    Linux系列——VMware虚拟机配置端口转发(端口映射),实现远程访问[通俗易懂]前言本文所写的远程为各个电脑在同一个网段内本文主要详细介绍如何远程访问虚拟机里面的项目!机器:虚拟机(装在电脑1里)、电脑1(宿主机)、电脑2、电脑3、电脑4、电脑n…操作步骤:step1:打开虚拟机,选择编辑–虚拟网络编辑器step2:选择VMnet8,点击NAT设置,弹出一个设置窗口step3:添加一个端口转发主机端口:随便选取一个宿主机没有被占用……

    2022年8月21日

发表回复

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

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