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


相关推荐

  • linux ad7705驱动程序,tm7705_51单机片驱动[通俗易懂]

    这是tm7705_51单机片驱动,为AD7705的驱动程序,keil开放环境开发。基于51单片机。软件说明tm770551驱动已在STC单片机51驱动AD77057705电路图TL431管脚图软件功能这是我自己做的AD7705采集模块,我毕业设计做的linux系统下面的前置采集,做的是AD7705的驱动的编写,下面是我编写的AD7705的51单片机的程序,直接调试完成了,你们可以直接…

    2022年4月12日
    87
  • 【杀软】Win7内置恶意软件删除工具——MRT

    【杀软】Win7内置恶意软件删除工具——MRTWindows7的WindowsDefender是一款具备实时监控能力的专业恶意软件清除工具。可能许多人将其禁用了,换用其他具备恶意软件清除能力的世界顶级杀软了(比如说笔者就用的NOD32,对恶意软件查杀能力甚是强悍)。如果你不忍WindowsDefender占用大量内存,又不舍得丢弃微软提供的服务,如何是好?有一个折中的方案:使用与其具有相同资源库的MRT…

    2022年6月24日
    88
  • 用户态和内核态:用户态线程和内核态线程有什么区别?[通俗易懂]

    用户态和内核态:用户态线程和内核态线程有什么区别?[通俗易懂]转载文章来源于拉钩教育重学操作系统林䭽用户态和内核态:用户态线程和内核态线程有什么区别?什么是用户态和内核态Kernel运行在超级权限模式(SupervisorMode)下,所以拥有很高的权限。按照权限管理的原则,多数应用程序应该运行在最小权限下。因此,很多操作系统,将内存分成了两个区域:内核空间(KernalSpace),这个空间只有内核程序可以访问;用户空间(UserSpace),这部分内存专门给应用程序使用。用户态和内核态用户空间中的代码被限制了只能使用一

    2022年9月16日
    0
  • 爱发php企业发卡网源码_爱发个人版自动发卡平台PHP开源源码【官方正版】

    爱发php企业发卡网源码_爱发个人版自动发卡平台PHP开源源码【官方正版】爱发个人版自动发卡平台PHP开源网站源码是我官方研发出个人版自动发卡平台系统演示站:http://ka.yc88.net备用:http://php2.a8tg.com/后台:http://php2.a8tg.com/admin.php服务器环境:PHP5.3、zend3、mysql5.1以上版本空间要支持.htaccess伪静态使用自动发卡平台禁止销售国家违规商品,被有关机关查处或接口被冻结或资金…

    2022年7月14日
    14
  • 国内免费php mysql空间,[php mysql]国内有什么好的免备案免费php+mysql空间

    国内免费php mysql空间,[php mysql]国内有什么好的免备案免费php+mysql空间国内有什么好的免备案免费php+mysql空间问题补充:稳定点的,速度过得去就可以.谢谢●我一直在用”主机屋”提供的免费空间.稳定性可以运作几年了.不用备案.用了马上就知道好.百度搜索”主机屋”php+mysql实现无限级分类问题补充:php+mysql实现无限级分类●项目思路分析:一个PHP项目要用到分类,但不确定分几级,所以就想做成无限级分类。一开始想是按以前一样,数据库建4个值,如下:…

    2022年6月29日
    27
  • 网易云音乐如何将多个账号的音乐合并到一个账号

    网易云音乐如何将多个账号的音乐合并到一个账号

    2021年5月17日
    714

发表回复

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

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