Change a private static final field using Java reflection
Codemia Knowledge Hub ·
Java reflection can modify a private static final field, but how depends on your JDK. On JDK 8 and earlier you can strip the final modifier through the modifiers field hack. JDK 9 through 11 still allow it with warnings. JDK 12 removed the modifiers field from java.lang.reflect.Field, and from JDK 16 strong encapsulation blocks the workaround by default.
So the technique is only reliable on older JDKs. On modern Java you need VarHandle, MethodHandles.Lookup or --add-opens flags, and even those are increasingly restricted. The history matters because a lot of legacy code and Stack Overflow answers still use the old approach.
Why people want to do this
- Testing: overriding a constant (a timeout, feature flag or endpoint URL) in unit tests without touching production code.
- Legacy codebases: changing configuration constants in third-party libraries that expose no setter.
- Framework internals: some frameworks (older versions of Spring, Guice) use reflection to inject values into final fields.
It is always a workaround, not a recommended pattern. If you control the code, pass configuration through constructors or configuration objects instead.
The modifiers hack (JDK 8 and earlier)
This is the technique most tutorials show. It reads the modifiers field of java.lang.reflect.Field itself and clears the FINAL bit:
On JDK 8 this prints:
| Step | API Call | Purpose |
| 1. Get the target field | getDeclaredField("CONFIG_URL") | Access the field regardless of visibility |
| 2. Make it accessible | field.setAccessible(true) | Bypass private access check |
3. Get the modifiers field | Field.class.getDeclaredField("modifiers") | Access Field's own internal modifiers int |
4. Strip FINAL | modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL) | Clear the final bit using bitwise AND with NOT |
| 5. Set new value | field.set(null, "https://test.example.com") | First arg is null because the field is static |
Compile-time constants are inlined
Primitive and String constants have a critical catch. When the value is known at compile time, the compiler inlines it into every place it is used. Reflection changes the field, but the inlined copies keep the old value.
This applies to String literals and to int, long, double, float, boolean, char, short and byte fields assigned a literal or constant expression. The reflective view of the field changes, but the compiled bytecode already has the old value baked in.
To avoid it, initialize the field with a non-constant expression:
Behavior by JDK version
| JDK Version | modifiers Field Hack | VarHandle | --add-opens Required | Notes |
| 8 and earlier | Works | N/A | No | No warnings |
| 9 to 11 | Works with warnings | Available (JDK 9+) | No (but recommended) | Illegal reflective access warning |
| 12 to 15 | Fails (NoSuchFieldException) | Works with --add-opens | Yes for cross-module access | modifiers field removed from Field |
| 16+ | Fails | Restricted by default | Yes, mandatory | Strong encapsulation enforced |
Options on modern JDKs
VarHandle (JDK 9+)
VarHandle gives a cleaner field-access API, but for final fields in modules you don't own it still needs --add-opens:
The --add-opens JVM flag
On JDK 16+ you must pass explicit module-opening flags:
Without them privateLookupIn fails with IllegalAccessException, because java.base does not open java.lang.reflect.
Test frameworks instead of reflection
For tests, the most common case, frameworks offer safer options:
These avoid reflection completely and are unaffected by JDK changes.
When reflecting on final fields is justified
Rarely, but there are legitimate cases:
- Deserializers and ORM frameworks that rebuild objects from external data must set final fields during construction. Gson and Jackson use
setAccessibleinternally for this. - Security research and instrumentation tools need deep reflective access by design.
- Legacy integration where the source can't change and there is no configuration API.
Otherwise, redesign the code to take values through constructors, configuration objects or dependency injection.
Mistakes to avoid
- Expecting compile-time constants to change everywhere.
static finalprimitives and String literals are inlined by the compiler, so every compiled use site keeps the old value even after reflection changes the field. - Testing on JDK 8 and deploying on JDK 17+. The hack works on 8, throws
NoSuchFieldExceptionon 12+ andInaccessibleObjectExceptionon 16+. Test reflective code on your production JDK. - Using
field.set(null, value)for instance fields.nullworks only for static fields; for instance fields pass the object. - Not restoring values after tests. A modified
static finalfield leaks into later tests in the same JVM and makes the suite flaky. - Using
getFieldinstead ofgetDeclaredField.getFieldreturns only public fields; private fields needgetDeclaredField.
Related reading
- Changing Java Date one hour back
- Changing the current working directory in Java?
- CharSequence VS String in Java?
- Check for the existence of a Kafka topic programatically in Java
- Check if a Class Object is subclass of another Class Object in Java
- Check if a value exists in ArrayList
- Check if null Boolean is true results in exception
- Check two arguments in Java, either both not null or both null elegantly
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.