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


相关推荐

  • 解析PHP的self关键字

    解析PHP的self关键字

    2022年2月8日
    45
  • HLSL 与 GLSL 之间的映射关系参考[通俗易懂]

    HLSL 与 GLSL 之间的映射关系参考[通俗易懂]系统参数与內建的输入参数Direct3D有很多系统参数,而相应的,GLSL也有內建的输入参数的概念。他们的对应关系如下:HLSLGLSLSV_ClipDistancegl_ClipDistanceSV_CullDistance当存在ARB_cull_distance情况下gl_CullDistanceSV_Coveragegl_SampleMaskIn&gl_SampleMaskSV_Depth

    2022年7月15日
    19
  • Html5_禁止Html5在手机上屏幕页面缩放

    Html5_禁止Html5在手机上屏幕页面缩放

    2021年9月10日
    59
  • Vue电商后台管理系统功能展示

    Vue电商后台管理系统功能展示项目技术:html、css、javascript、node.js、vue、mysql项目描述:电商后台管理系统,主要功能包括:用户账号管理、商品分类、商品信息、订单、数据统计等业务功能。采用前后端分离的开发模式,其中前端项目是基于Vue技术栈的SPA项目,通过API接口连接服务器,访问mysql数据库。成品效果展示:一、登陆界面二、登陆校验规则三、登陆成功后,保存Token四、用户管理页面五、添加用户功能(采用正则表达式进行校验)六、添加用户成功,弹出消息框.

    2022年5月5日
    53
  • ICMP报文格式解析

    ICMP报文格式解析ICMP报文的格式类型总共分为三大类:1、差错报文2、控制报文3、查询报文上图是ICMP报文的基本格式,上面提到的三种ICMP报文均有“类型,代码和校验和”三个字段,后面还有4个字节是根据不同的报文类型而有不同的格式,有的是全0,有的则有其他的特殊格式。但是ICMP始终有8个字节的头部长度。其中类型字段代表着不同的报文类型,而代码字段指明了某个类型的报文中细分出的该报文的指定的功能。即一个类型的报文拥有着多种功能。同时还需要注意的是ICMP差错报文的数据部分存储的是IP头部和IP头.

    2022年5月5日
    93
  • 最新最详细的Python开发环境搭建以及PyCharm的安装配置教程【图+文】[通俗易懂]

    最新最详细的Python开发环境搭建以及PyCharm的安装配置教程【图+文】[通俗易懂]本文为长文:Python进阶之道【阶段一】的第一章,截取本章单独发布是因为帮助更多读者进行专项的需求,本文采用图+文的形式,配图众多,每步都很详细,且下载为最新Python3.10,和最新的PyCharm,安装PyCharm分为付费版和免费版的安装演示。

    2022年8月26日
    9

发表回复

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

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