- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一种情况,我必须更改 java 常量。
我有下面的代码工作
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Main {
public static final Integer FLAG = 44;
static void setFinalStatic(Class<?> clazz, String fieldName, Object newValue) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Field modifiers = field.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
public static void main(String... args) throws Exception {
System.out.printf("Everything is %s%n", FLAG);
setFinalStatic(Main.class, "FLAG", 33);
System.out.printf("Everything is %s%n", FLAG);
}
}
如果我在上面运行,我得到以下输出:
Everything is 44
Everything is 33
但是如果我将 FLAG
变量更改为 int 即
public static final int FLAG = 44;
它不起作用。输出是:
Everything is 44
Everything is 44
有没有其他方法可以让它与 Primitive
Type int
一起工作。
最佳答案
来自 jls-4.12.4
A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a
constant variable
.
还有节13.1说(强调我的)
3..References to fields that are constant variables (§4.12.4) are resolved at compile time to the constant value that is denoted. No reference to such a field should be present in the code in a binary file (except in the class or interface containing the field, which will have code to initialize it). Such a field must always appear to have been initialized (§12.4.2); the default initial value for the type of such a field must never be observed.
这意味着 常量变量 中的编译时常量表达式 将由编译器直接放入代码中(它将被内联),而不是在运行时从最终引用中读取。
例如,如果您从 Bar
类执行 main
方法
class Foo{
static{
System.out.println("test if class will be loaded");
}
public static final int x = 42;
}
class Bar{
public static void main(String [] args){
System.out.println(Foo.x);
}
}
你会看到 Foo
类的静态 block 没有输出,这意味着 Foo
类还没有被加载,这意味着 Foo.x
不是来自此类。事实上 Bar
就是这样编译的
class Bar{
public static void main(String [] args){
System.out.println(42); // reference Foo.x will be removed by compiler
// and replaced with actual value because
// compiler assumes that value can't/shouldn't
// change at runtime
}
}
因此,即使在运行时更改 Foo.x
的值也不会影响 Bar
类的 main
方法中打印的值。
您无法更改该机制。
可能的解决方法是使用在运行时创建的值初始化您的最终字段(它们在编译时不存在,这将防止它们成为编译时常量表达式)。所以不是
public static final String x = "foo";
尝试
public static final String x = new String("foo");
或者在原始类型的情况下使用 Unboxing 而不是
public static final int x = 42;
使用
public static final int x = new Integer(42);
关于java - 更改静态变量适用于 Primitive Wrapper 但不适用于 Primitive 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273936/
我假设 jls 中描述的转换是根据优先级排序的。首先具有更高的优先级。 jls 因此我解决了 Boxing 比 Unboxing 具有更高的优先级。我决定检验这个假设。 研究以下代码: public
谁能解释一下我的 html 设计出了什么问题 Profile 问题是 div 标签(#profile wrapper)正在增加它的 margin top,如果我增加 #intro w
下面的完整代码示例(已成功编译)是我的问题的简化且稍微做作的示例。 NatPair 是一对 Nat ,我想使用函数 Num 将二进制 NatPair 操作逐点“提升”到 lift_binary_op_
给定以下函数: public void convertToWrapper(long[] longsToConvert) { } 和 public void convertToPrimitiv
我有这样的代码: class Base { } class Derived : Base { } class Wrapper { public T Value { get; } pub
我正在安装 SonarQube v5.0。 我正在运行 Windows Server 2012 64 位(虚拟操作系统)、Java 1.8 64 位和 SonarQube windows-x86-64
我正在为我的一个组件编写测试用例,该组件具有路由器(使用 withrouter)。我收到错误 wrapper.find is not a function。基本要求是需要检查我的渲染中是否存在标签,还
如何制作 在不改变结构的情况下,内部包装器比包装器本身更大? HTML CSS .page { width: 100%; height: 400px; bor
为了引导 Gradle-Wrapper,我需要从需要 HTTP Basic-Auth 的 Artifactory 中提取 Gradle 发行版。我的构建环境无法访问外部世界 - 这被公司代理阻止。我的
我正在构建一个Spring-Boot应用程序(2.1.4.RELEASE),并且正在使用它创建一个Gradle Wrapper gradle clean wrapper 我的gradle版本是5.4
以下代码取自这篇文章:How to create Scala swing wrapper classes with SuperMixin? import scala.swing._ import ja
我想同时使用 Boost.Asio 的 strand 和 prioritized wrapper。 在我编写代码之前,我阅读了以下信息: Boost asio priority and strand
Android Studio 的 Gradle 选项到底有什么区别: Android Studio->Preferences->Gradle 使用默认的gradle wrapper(推荐)和使用可定制
目前我有一个设置宽度为 1240 像素的包装器。可以想象,这意味着我页面中心的 1240 像素始终被包装器覆盖。 我还有一张宽度为 3160 像素的图像(1920 像素的图像大小,在中间切割并由 12
这只是一个练习,但我无法弄清楚其中的歧义: private static void flipFlop(String str, int i, Integer iRef) { System.out.pri
所以我现在正在编写我的网站,我正在使用两个 div 在背景中创建某种渐变,其中 div#bg1 为灰色,div#bg2 为深灰色。但是当我添加包装器 div 时,bg1 和 bg2 仍然遵循包装器的顶
已解决:必须 float #main div,并对结构进行许多其他重大更改。但致命一击是 float 的。 可以在 http://thepremium.ca/amodestblog 查看有问题的网站
操作系统:Windows 8.1 Visual Studio 高级版 2013 我有一个复杂的 MVC 应用程序,我已经运行了多年。我能够毫无问题地在调试中运行应用程序。然而,就在今天下午,当我尝试发
在带有 python 3.6.8 的 Ubuntu 18.04 上,尝试安装 Airflow。当我运行airflow initdb命令时,抛出以下错误 Traceback (most recent c
我将我的 Android Studio 升级到 2.3 版,然后开始出现此错误: Error:org.gradle.wrapper.WrapperExecutor.forProjectDirector
我是一名优秀的程序员,十分优秀!