I have a class with a private static final field that, unfortunately, I need to change it at run-time.
Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field
Is there any way to change the value?
Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK"); hack.setAccessible(true); hack.set(null, true);
In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.
Finally, with the static final variable, it's both the same for each class and it can't be changed after it's initialized.
If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.
Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods.
Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final, and actually modify a private static final field.
Here's an example:
import java.lang.reflect.*; public class EverythingIsTrue { static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } public static void main(String args[]) throws Exception { setFinalStatic(Boolean.class.getField("FALSE"), true); System.out.format("Everything is %s", false); // "Everything is true" } } Assuming no SecurityException is thrown, the above code prints "Everything is true".
What's actually done here is as follows:
boolean values true and false in main are autoboxed to reference type Boolean "constants" Boolean.TRUE and Boolean.FALSE public static final Boolean.FALSE to refer to the Boolean referred to by Boolean.TRUE false is autoboxed to Boolean.FALSE, it refers to the same Boolean as the one refered to by Boolean.TRUE "false" now is "true" static final File.separatorChar for unit testingInteger's cache, mutating a String, etcExtreme care should be taken whenever you do something like this. It may not work because a SecurityManager may be present, but even if it doesn't, depending on usage pattern, it may or may not work.
JLS 17.5.3 Subsequent Modification of Final Fields
In some cases, such as deserialization, the system will need to change the
finalfields of an object after construction.finalfields can be changed via reflection and other implementation dependent means. The only pattern in which this has reasonable semantics is one in which an object is constructed and then thefinalfields of the object are updated. The object should not be made visible to other threads, nor should thefinalfields be read, until all updates to thefinalfields of the object are complete. Freezes of afinalfield occur both at the end of the constructor in which thefinalfield is set, and immediately after each modification of afinalfield via reflection or other special mechanism.Even then, there are a number of complications. If a
finalfield is initialized to a compile-time constant in the field declaration, changes to thefinalfield may not be observed, since uses of thatfinalfield are replaced at compile time with the compile-time constant.Another problem is that the specification allows aggressive optimization of
finalfields. Within a thread, it is permissible to reorder reads of afinalfield with those modifications of a final field that do not take place in the constructor.
private static final boolean, because it's inlineable as a compile-time constant and thus the "new" value may not be observableEssentially,
field.getModifiers() & ~Modifier.FINAL turns off the bit corresponding to Modifier.FINAL from field.getModifiers(). & is the bitwise-and, and ~ is the bitwise-complement.
Still not being able to solve this?, have fallen onto depression like I did for it? Does your code looks like this?
public class A { private final String myVar = "Some Value"; } Reading the comments on this answer, specially the one by @Pshemo, it reminded me that Constant Expressions are handled different so it will be impossible to modify it. Hence you will need to change your code to look like this:
public class A { private final String myVar; private A() { myVar = "Some Value"; } } if you are not the owner of the class... I feel you!
For more details about why this behavior read this?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With