简体   繁体   English

通过反射改变最终变量,为什么静态和非静态最终变量之间存在差异

[英]changing final variables through reflection, why difference between static and non-static final variable

Please refer to the below code. 请参考以下代码。 When I run the code, I am able to change the value of a final non-static variable. 当我运行代码时,我能够更改最终的非静态变量的值。 But if I try to change the value of a final static variable then it throws java.lang.IllegalAccessException . 但是如果我尝试更改最终静态变量的值,那么它会抛出java.lang.IllegalAccessException

My question is why doesn't it throw an exception in case of non-static final variable also or vice versa. 我的问题是为什么它不会在非静态最终变量的情况下抛出异常,反之亦然。 Why the difference? 为什么不同?

import java.lang.reflect.Field;
import java.util.Random;

public class FinalReflection {

    final static int stmark =  computeRandom();
    final int inmark = computeRandom();

    public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        FinalReflection obj = new FinalReflection();
        System.out.println(FinalReflection.stmark);
        System.out.println(obj.inmark);
        Field staticFinalField  = FinalReflection.class.getDeclaredField("stmark");
        Field instanceFinalField  = FinalReflection.class.getDeclaredField("inmark");
        staticFinalField.setAccessible(true);
        instanceFinalField.setAccessible(true);

        instanceFinalField.set(obj, 100);
        System.out.println(obj.inmark);

        staticFinalField.set(FinalReflection.class, 101);
        System.out.println(FinalReflection.stmark);

    }

    private static int computeRandom() {
        return new Random().nextInt(5);
    }
}
FinalReflectionobj = new FinalReflection();
System.out.println(FinalReflection.stmark);
System.out.println(obj.inmark);
Field staticFinalField  = FinalReflection.class.getDeclaredField("stmark");
Field instanceFinalField  = FinalReflection.class.getDeclaredField("inmark");
staticFinalField.setAccessible(true);
instanceFinalField.setAccessible(true);

//EXTRA CODE
//Modify the final using reflection
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(staticFinalField, staticFinalField.getModifiers() & ~Modifier.FINAL);


instanceFinalField.set(obj, 100);
System.out.println(obj.inmark);
staticFinalField.set(FinalReflection.class, 101);
System.out.println(FinalReflection.stmark);

This solution does not come without some downsides, it may not work in all cases: 这个解决方案没有一些缺点,它可能不适用于所有情况:

In case a final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be visible, since uses of that final field are replaced at compile time with the compile-time constant. 的情况下的final场被初始化为一个编译时在字段声明恒定,改变到final字段可能是不可见的,因为这最后字段的用途是在编译时与编译时间常数替代。

Another problem is that the specification allows aggressive optimization of final fields. 另一个问题是规范允许对final字段进行积极优化。 Within a thread, it is permissible to reorder reads of a final field with those modifications of a final field that do not take place in the constructor. 在一个线程,它允许将重新排序的读取final用的这些修改场final没有发生在构造领域。 More on this is also explained in this similar question. 在这个类似的问题中也解释了更多相关问题。

The javadoc is clear: javadoc很清楚:

If the underlying field is final, the method throws an IllegalAccessException unless setAccessible(true) has succeeded for this Field object and the field is non-static. 如果基础字段是final,则该方法抛出IllegalAccessException,除非此Field对象的setAccessible(true)成功并且该字段是非静态的。

From a JLS perspective, the exact behaviour of how reflection should work is not specified, but in JLS 17.5.4 : 从JLS的角度来看,没有指定反射应该如何工作的确切行为,但是在JLS 17.5.4中

Normally, a field that is final and static may not be modified. 通常,可能不会修改最终和静态字段。

One workaround is to remove the final modifier through reflection . 一种解决方法是通过反射删除最终修改器

For final, it can be assigned different values at runtime when initialized. 对于final,初始化时可以在运行时为其分配不同的值。

Class Test{    
public final int a;
}

Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t1.a = 20;

Thus each instance has different value of field a. 因此,每个实例具有不同的字段a值。

For static final, all instances share the same value, and can't be altered after first initialized. 对于静态final,所有实例共享相同的值,并且在首次初始化后不能更改。

Class TestStatic{
   public static final int a;
}

Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t1.a = 20;   // ERROR, CAN'T BE ALTERED AFTER THE FIRST INITIALIZATION.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM