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

后端 未结 3 1506
清歌不尽
清歌不尽 2020-12-31 17:54

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 variab

3条回答
  •  佛祖请我去吃肉
    2020-12-31 18:34

    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.

    Another problem is that the specification allows aggressive optimization of final fields. 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. More on this is also explained in this similar question.

提交回复
热议问题