Dynamically change private static final fields in Java

Overview

In Java, a method of setAccessible (true) using reflection is widely known as a means of changing the value of a private field that is normally inaccessible from the outside.

We will show you how to apply this method to dynamically change the final field.

Sample code

Main.java


public class Main {

    public static void main(String[] args) throws Exception {

        TargetClass clazz = new TargetClass();
        System.out.println("Before update: " + clazz.getTargetField());                    // Before update: 1

        Field targetField = clazz.getClass().getDeclaredField("TARGET_FIELD");     //Get the Field object for the access to be updated.
        Field modifiersField = Field.class.getDeclaredField("modifiers");          //Since the Field class uses modifiers to determine the access of the field to be accessed, update this.
        modifiersField.setAccessible(true);                                        //The modifiers themselves are private, so make them accessible.
        modifiersField.setInt(targetField,
                targetField.getModifiers() & ~Modifier.PRIVATE & ~Modifier.FINAL); //Remove private and final from the modifiers of the Field object for update target access.
        targetField.set(null, 1000);                                               //Value update: 1 -> 1000

        System.out.println("_People People People People_");                                  // _People People People People_
        System.out.println("After update: >  " + clazz.getTargetField() + "  <");       // After update: >  1000  <
        System.out.println("        ̄Y^Y^Y^Y ̄");                                     //        ̄Y^Y^Y^Y ̄

    }
}

TargetClass.java


public class TargetClass {

    private static final Integer TARGET_FIELD = 1;

    public Integer getTargetField() {
        return TARGET_FIELD;
    }

}

Supplement

In an environment where SecurityManager is enabled, private static final fields cannot be changed dynamically using the above method. Also, if you are inlined by compile-time optimization, you cannot change the value using the above method. (In this case, no error will occur.)

reference

http://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection

Recommended Posts

Dynamically change private static final fields in Java
[Java] Difference between static final and final in member variables
Change java encoding in windows
Difference between final and Immutable in Java
Java static
JAVA learning history final modifier and static modifier
Use static initialization block to initialize List / Set of static fields in Java
Change List <Optional <T >> to Optional <List <T >> in Java
[Swift vs Java] Let's understand static and final
[Java] Use of final in local variable declaration
How to hide null fields in response in Java
Check static and public behavior in Java methods
What I've always been interested in in Java final
Change paragraph text color in Java Word documents
How to access Java Private methods and fields
Java static story
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
java final modifier
FizzBuzz in Java
Change Java heap size in Tomcat in Azure App Service
Include image in jar file with java static method
A note for Initializing Fields in the Java tutorial
Change the storage quality of JPEG images in Java
When you want to dynamically replace Annotation in Java8