564

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);
Hearen
  • 7,420
  • 4
  • 53
  • 63
fixitagain
  • 6,543
  • 3
  • 20
  • 24
  • 4
    Such a bad idea. I'd try to get the source and recompile (or even decompile/recompile) instead. – Bill K Jul 21 '10 at 17:07
  • System.out is a public static final field, but it can be changed too. – irreputable Jul 21 '10 at 19:29
  • 21
    @irreputable `System.out/in/err` are so "special" that the Java Memory Model has to make special mention of them. They are not examples which should be followed. – Tom Hawtin - tackline Jul 21 '10 at 19:32
  • 8
    well my point ws to find a hack in the between to have my app working until the lib responsible make the change at the next release so i don't need to hack anymore... – fixitagain Jul 23 '10 at 08:31
  • 6
    @Bill K from ten years ago: It would be GREAT to recompile it but it's on a deployed system and I just need to patch it until we can update the deployed app! – Bill K Feb 06 '20 at 22:59
  • For Java 17 [Get declared fields of java.lang.reflect.Fields in jdk12](https://stackoverflow.com/a/71465198/432903) – prayagupa Oct 07 '22 at 18:39

14 Answers14

1010

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:

  • The primitive boolean values true and false in main are autoboxed to reference type Boolean "constants" Boolean.TRUE and Boolean.FALSE
  • Reflection is used to change the public static final Boolean.FALSE to refer to the Boolean referred to by Boolean.TRUE
  • As a result, subsequently whenever a false is autoboxed to Boolean.FALSE, it refers to the same Boolean as the one refered to by Boolean.TRUE
  • Everything that was "false" now is "true"

Related questions


Caveats

Extreme 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 final fields of an object after construction. final fields 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 the final fields of the object are updated. The object should not be made visible to other threads, nor should the final fields be read, until all updates to the final fields of the object are complete. Freezes of a final field occur both at the end of the constructor in which the final field is set, and immediately after each modification of a final field via reflection or other special mechanism.

Even then, there are a number of complications. If a final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be observed, 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.

See also

  • JLS 15.28 Constant Expression
    • It's unlikely that this technique works with a primitive private static final boolean, because it's inlineable as a compile-time constant and thus the "new" value may not be observable

Appendix: On the bitwise manipulation

Essentially,

field.getModifiers() & ~Modifier.FINAL

turns off the bit corresponding to Modifier.FINAL from field.getModifiers(). & is the bitwise-and, and ~ is the bitwise-complement.

See also


Remember Constant Expressions

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?

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 6
    When I saw this: `System.out.format("Everything is %s", false); // "Everything is true"` I thought you were screwing with us, then I read the rest of your code, but it brings up a readability question, some things your just accept as true (or in this case, false). – HalfBrian Jul 21 '10 at 17:21
  • 53
    @thecoop, @HalfBrian: there is no doubt that this is _EXTREMELY EVIL_, but this example was chosen by design. My answer only shows how, in some circumstances, this is possible. The most disgusting example that I can think of is deliberate chosen with the hope that perhaps people would be instantly disgusted by instead of falling in love with the technique. – polygenelubricants Jul 21 '10 at 17:24
  • 74
    Yo, dawg. I heard you like reflection, so I reflected on field so you can reflect while you reflect. – Matthew Flaschen Mar 22 '11 at 20:35
  • I want to use this for testing by replacing a singleton with a mock object without compromising the singleton in production code. – Yashima Oct 16 '12 at 14:26
  • 12
    Note that Boolean.FALSE is **not** private.Does this really work with "private final static" members? – mgaert Feb 15 '13 at 13:11
  • 20
    @mgaert it does, but you have to use `getDeclaredField()` instead of `getField()` for target class – eis Aug 14 '13 at 07:28
  • 15
    +1. For those who will try to change something like `final String myConstant = "x";` and will fail: remember that compile time constants will be inlined by compiler so when you will write code like `System.out.println(myConstant);` it will be compiled as `System.out.println("x");` because compiler knows value of constant at compile time. To get rid of this problem you need to initialize your constants at runtime like `final String myConstant = new String("x");`. Also in case of primitives like `final int myField = 11` use `final int myField = new Integer(11);` or `final Integer myField = 11;` – Pshemo Dec 02 '13 at 18:15
  • 4
    Note that you can check the `SecurityManager` beforehand to see if you're allowed to do this: `final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new java.lang.reflect.ReflectPermission("suppressAccessChecks")); }` – Matt Apr 20 '14 at 21:40
  • Appears it does not work with `static final` fields of primitive types even if they are initialized with something that is _not_ compile time constant (property access in my case). – Jan Hudec Jun 24 '14 at 18:28
  • I tried to use your code one Android, but modifiers said NoSuchField.. Android uses Java 1.7 I believe. – Maxr1998 Jun 26 '15 at 15:24
  • 7
    Never mind, looking at the Android source, I found out, that the "modifiers" field is "accessFlags" on Android. – Maxr1998 Jun 26 '15 at 15:49
  • Why this business with modifying bitmasks?.setAccessible(true) turns of all language checks anyways right? – ben Jun 03 '16 at 12:43
  • What is `"modifiers"`? Does it some kind of system field that is generated automatically by compiler? How this one is related to `field` method parameter (value of which we are trying to change)? – ieXcept Jun 21 '16 at 16:41
  • 1
    @ieXcept `modifiers` is a private member of the `Field` class. It is set to the appropriate bit pattern for the modifiers of the field represented by that particular `field` instance. – Matthew Read Dec 16 '16 at 22:40
  • 1
    Great for having a program changing its own environment (or, at least, the return value of System.getenv()). Why the environment is immutable is a question I am not wise enough to know the answer to. – Phil Sep 04 '17 at 10:56
  • 3
    Note that JLS §17.5.3 is referring to non-`static` `final` fields. Subsequent modification of `static` `final` fields is not foreseen at all and hence, entirely off specification. Anything can happen, including no effect at all or a complete crash of the JVM… – Holger Feb 09 '18 at 11:03
  • 3
    Another caveat is that you can't read the Field before removing the modifier or it gets cached: https://stackoverflow.com/q/49102935/1553851 – shmosel Mar 05 '18 at 06:08
  • Doesnt make sense why u got 2 fields – Philip Rego Jan 23 '19 at 16:20
  • 10
    In Java 12 this is no longer allowed. See bug 8210496 https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8210496 https://hg.openjdk.java.net/jdk-updates/jdk12u/rev/f55a4bc91ef4 – swpalmer Apr 25 '19 at 20:23
  • Does this works for the final static field of a static class ? I try but looks like the field of the static class was not changed as expected. – bubu0321 Sep 18 '19 at 19:57
  • 1
    Evil and working, thank you >:-D Unfortunately does not work on Java 13. – Martin Vysny Oct 17 '19 at 08:32
  • This worked for setting a `static final` logger to a mocked logger in a test. Thank you! – John Mercier Apr 03 '20 at 17:24
  • 3
    Mind (again) that “JLS 17.5.3 Subsequent Modification of Final Fields” applies to *instance fields* only. It describes the problems which may happen when you “change the final fields of an object after construction” whereas changing a `static final` field after the class initialization is not even foreseen by the specification. That’s why for a non-static `final` field a simple `setAccessible(true)` is enough to change it whereas changing a `static final` field requires deeper hacks which the JDK developers try to disable. Even if you find another work-around, it’ll break the next version. – Holger Apr 15 '20 at 08:46
  • As mentioned in Ans"Final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be observed" -any way to work on this when the fields is inbuilt @Holger – Suchith Jan 06 '21 at 11:14
  • 7
    @swpalmer Yep, not working in Java 12 anymore, check [this answer for the new](https://stackoverflow.com/a/56043252/2979435) trick – deFreitas Jan 17 '21 at 20:34
  • 1
    java.lang.NoSuchFieldException: modifiers – sproketboy Apr 04 '21 at 20:45
  • I am using this solution in my testing and I want to tear down the changes done to the variable. How can I revert (put back final modifier back) setFinalStatic method changes once the testing is done ? – Subhajit Jul 27 '21 at 02:44
  • So if I wanted to make the field final again, would I just omit the tilde `~`? – Cardinal System Sep 13 '21 at 16:49
  • 3
    I wonder how this could work on JDK 17. I think `modifiers` is not accessible anymore. – JeanValjean Jan 20 '22 at 10:36
  • 1
    not working on 17 – Tarik Mar 07 '23 at 19:49
66

If the value assigned to a static final boolean field is known at compile-time, it is a constant. Fields of primitive or String type can be compile-time constants. A constant will be inlined in any code that references the field. Since the field is not actually read at runtime, changing it then will have no effect.

The Java language specification says this:

If a field is a constant variable (§4.12.4), then deleting the keyword final or changing its value will not break compatibility with pre-existing binaries by causing them not to run, but they will not see any new value for the usage of the field unless they are recompiled. This is true even if the usage itself is not a compile-time constant expression (§15.28)

Here's an example:

class Flag {
  static final boolean FLAG = true;
}

class Checker {
  public static void main(String... argv) {
    System.out.println(Flag.FLAG);
  }
}

If you decompile Checker, you'll see that instead of referencing Flag.FLAG, the code simply pushes a value of 1 (true) onto the stack (instruction #3).

0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3:   iconst_1
4:   invokevirtual   #3; //Method java/io/PrintStream.println:(Z)V
7:   return
erickson
  • 265,237
  • 58
  • 395
  • 493
  • That was my first thought, but then I remembered Java compiled at runtime, if you were to reset the bit it would simply recompile with it as a variable instead of a constant. – Bill K Jul 21 '10 at 17:05
  • 4
    @Bill K - No, this doesn't refer to JIT compilation. The dependent class files will actually contain the inlined values, and no reference to the independent class. It's a pretty simple experiment to test; I will add an example. – erickson Jul 21 '10 at 17:20
  • 1
    How does this jive with @polygenelubricants 's answer where he redefines Boolean.false?--but you are correct, I've seen this behavior when things didn't recompile correctly. – Bill K Jul 21 '10 at 17:31
  • 28
    @Bill K - in polygenlubricants' answer, the field is not a compile time constant. It's `public static final Boolean FALSE = new Boolean(false)` not `public static final boolean FALSE = false` – erickson Jul 21 '10 at 18:07
20

A little curiosity from the Java Language Specification, chapter 17, section 17.5.4 "Write-protected Fields":

Normally, a field that is final and static may not be modified. However, System.in, System.out, and System.err are static final fields that, for legacy reasons, must be allowed to be changed by the methods System.setIn, System.setOut, and System.setErr. We refer to these fields as being write-protected to distinguish them from ordinary final fields.

Source: http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.4

10

I also integrated it with joor library

Just use

      Reflect.on(yourObject).set("finalFieldName", finalFieldValue);

Also I fixed an issue with override which the previous solutions seem to miss. However use this very carefully, only when there's no other good solution.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
iirekm
  • 8,890
  • 5
  • 36
  • 46
7

Along with top ranked answer you may use a bit simpliest approach. Apache commons FieldUtils class already has particular method that can do the stuff. Please, take a look at FieldUtils.removeFinalModifier method. You should specify target field instance and accessibility forcing flag (if you play with non-public fields). More info you can find here.

nndru
  • 2,057
  • 21
  • 16
  • This is a much simpler solution than the currently accepted answer – Bernie Oct 27 '17 at 05:01
  • 5
    Is it? Copying one method sounds like a simpler solution than importing an entire library (which is doing the same thing as the method you'd be copying). – eski Jun 05 '18 at 14:34
  • 3
    Does not work in Java 12+: `java.lang.UnsupportedOperationException: In java 12+ final cannot be removed.` – MrPowerGamerBR Jan 31 '20 at 01:24
7

Even in spite of being final a field can be modified outside of static initializer and (at least JVM HotSpot) will execute the bytecode perfectly fine.

The problem is that Java compiler does not allow this, but this can be easily bypassed using objectweb.asm. Here is p̶e̶r̶f̶e̶c̶t̶l̶y̶ ̶v̶a̶l̶i̶d̶ ̶c̶l̶a̶s̶s̶f̶i̶l̶e̶ an invalid classfile from the JVMS specification standpoint, but it passes bytecode verification and then is successfully loaded and initialized under JVM HotSpot OpenJDK12:

ClassWriter cw = new ClassWriter(0);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Cl", null, "java/lang/Object", null);
{
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "fld", "I", null, null);
    fv.visitEnd();
}
{
    // public void setFinalField1() { //... }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "setFinalField1", "()V", null, null);
    mv.visitMaxs(2, 1);
    mv.visitInsn(Opcodes.ICONST_5);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, "Cl", "fld", "I");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitEnd();
}
{
    // public void setFinalField2() { //... }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "setFinalField2", "()V", null, null);
    mv.visitMaxs(2, 1);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, "Cl", "fld", "I");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitEnd();
}
cw.visitEnd();

In Java, the class looks roughly speaking as follows:

public class Cl{
    private static final int fld;

    public static void setFinalField1(){
        fld = 5;
    }

    public static void setFinalField2(){
        fld = 2;
    }
}

which cannot be compiled with javac, but can be loaded and executed by JVM.

JVM HotSpot has special treatment of such classes in the sense that it prevents such "constants" from participating in constant folding. This check is done on the bytecode rewriting phase of class initialization:

// Check if any final field of the class given as parameter is modified
// outside of initializer methods of the class. Fields that are modified
// are marked with a flag. For marked fields, the compilers do not perform
// constant folding (as the field can be changed after initialization).
//
// The check is performed after verification and only if verification has
// succeeded. Therefore, the class is guaranteed to be well-formed.
InstanceKlass* klass = method->method_holder();
u2 bc_index = Bytes::get_Java_u2(bcp + prefix_length + 1);
constantPoolHandle cp(method->constants());
Symbol* ref_class_name = cp->klass_name_at(cp->klass_ref_index_at(bc_index));
if (klass->name() == ref_class_name) {
   Symbol* field_name = cp->name_ref_at(bc_index);
   Symbol* field_sig = cp->signature_ref_at(bc_index);

   fieldDescriptor fd;
   if (klass->find_field(field_name, field_sig, &fd) != NULL) {
      if (fd.access_flags().is_final()) {
         if (fd.access_flags().is_static()) {
            if (!method->is_static_initializer()) {
               fd.set_has_initialized_final_update(true);
            }
          } else {
            if (!method->is_object_initializer()) {
              fd.set_has_initialized_final_update(true);
            }
          }
        }
      }
    }
}

The only restriction that JVM HotSpot checks is that the final field should not be modified outside of the class that the final field is declared at.

Some Name
  • 8,555
  • 5
  • 27
  • 77
  • 4
    this is just pure `EVIL` and beautiful. – Eugene Oct 09 '20 at 18:54
  • 4
    I don’t agree with the “perfectly valid classfile”. The [JVMS §6.5](https://docs.oracle.com/javase/specs/jvms/se15/html/jvms-6.html#jvms-6.5.putstatic) clearly says: “Otherwise, if the resolved field is final, it must be declared in the current class or interface, **and the instruction must occur in the class or interface initialization method** of the current class or interface. Otherwise, an IllegalAccessError is thrown”. So this is just another case where the implementation blatantly violates the specification and has code spread over multiple places to handle what should have been rejected – Holger Jan 06 '21 at 12:01
  • @Holger Thanks for the note. I made a correction according to your note to not confuse further readers. – Some Name Jan 13 '21 at 10:01
6

In case of presence of a Security Manager, one can make use of AccessController.doPrivileged

Taking the same example from accepted answer above:

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");

        // wrapping setAccessible 
        AccessController.doPrivileged(new PrivilegedAction() {
            @Override
            public Object run() {
                modifiersField.setAccessible(true);
                return null;
            }
        });

        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"
    }
}

In lambda expression, AccessController.doPrivileged, can be simplified to:

AccessController.doPrivileged((PrivilegedAction) () -> {
    modifiersField.setAccessible(true);
    return null;
});
VanagaS
  • 3,130
  • 3
  • 27
  • 41
  • yes @dan1st, you are right! Please check this for a solution: https://stackoverflow.com/a/56043252/2546381 – VanagaS Apr 11 '20 at 17:47
4

With JDK 18 this won't be possible anymore due to the reimplementation of the core reflection over invokedynamic and MethodHandles as part of JEP-416 (PR).

Quote of Mandy Chung – who is the main author of this incredible work – in the following comment. Emphasis are mine.

If the underlying field is final, a Field object has write access if and only if

  • setAccessible(true) has succeeded for this Field object;
  • the field is non-static; and
  • the field's declaring class is not a hidden class; and
  • the field's declaring class is not a record class.
bric3
  • 40,072
  • 9
  • 91
  • 111
3

Since Java 12 onwards, the answers given will not work.

Here is an example on how to modify a private static final field since Java 12 (based on this answer).

  private Object modifyField(Object newFieldValue, String fieldName, Object classInstance) throws NoSuchFieldException, IllegalAccessException {
    Field field = classInstance.getClass().getDeclaredField(fieldName);
    VarHandle MODIFIERS;

    field.setAccessible(true);

    var lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
    MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
    int mods = field.getModifiers();

    if (Modifier.isFinal(mods)) {
      MODIFIERS.set(field, mods & ~Modifier.FINAL);
    }

    Object previousValue = field.get(classInstance);
    field.set(null, newFieldValue);

    return previousValue;
  }

See this thread for more details.

pringi
  • 3,987
  • 5
  • 35
  • 45
  • 1
    This does not work with Java 16+. – Johannes Kuhn Sep 28 '21 at 21:43
  • @JohannesKuhn works if you add `--illegal-access=permit` – barwnikk Oct 30 '21 at 23:39
  • 1
    `--illegal-access=permit` was removed in Java 17. – Johannes Kuhn Oct 31 '21 at 00:10
  • 1
    There is no way to make this work with Java 18 due to [JEP 416](https://openjdk.java.net/jeps/416) – Johannes Kuhn Nov 30 '21 at 15:40
  • Even in Java 18, [JLS 17.5.3](https://docs.oracle.com/javase/specs/jls/se18/html/jls-17.html#jls-17.5.3) still says "final fields can be changed via reflection and other implementation-dependent means". So what is the reflection mechanism to satisfy the JLS? – Chris Rankin Apr 10 '22 at 19:29
  • I managed to use `lookup.findSetter(Field.class, "modifiers", int.class)` successfully on both Java 16 and Java 17. However, it still fails on Java 18 because `Field.modifiers` has suddenly become a `final` field. – Chris Rankin Apr 10 '22 at 19:32
  • @ChrisRankin The mechanism for non-static `final` fields is reflection with `setAccessible`. – Johannes Kuhn Apr 10 '22 at 21:59
  • OK, and what is the mechanism for `static final` fields? Because that's the case I'm actually interested in. Specifically - non-primitive `static final` fields, i.e. object references. – Chris Rankin Apr 11 '22 at 23:13
  • 1
    You can't change `static final` fields. [That never really worked](https://gist.github.com/DasBrain/ca16053f8e073c8a883bee66302c2fee). It may appear to work, but in the end - relying on that effect makes your code just non-portable. – Johannes Kuhn Apr 12 '22 at 13:06
2

Just saw that question on one of the interview question, if possible to change final variable with reflection or in runtime. Got really interested, so that what I became with:

 /**
 * @author Dmitrijs Lobanovskis
 * @since 03/03/2016.
 */
public class SomeClass {

    private final String str;

    SomeClass(){
        this.str = "This is the string that never changes!";
    }

    public String getStr() {
        return str;
    }

    @Override
    public String toString() {
        return "Class name: " + getClass() + " Value: " + getStr();
    }
}

Some simple class with final String variable. So in the main class import java.lang.reflect.Field;

/**
 * @author Dmitrijs Lobanovskis
 * @since 03/03/2016.
 */
public class Main {


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

        SomeClass someClass = new SomeClass();
        System.out.println(someClass);

        Field field = someClass.getClass().getDeclaredField("str");
        field.setAccessible(true);

        field.set(someClass, "There you are");

        System.out.println(someClass);
    }
}

The output will be as follows:

Class name: class SomeClass Value: This is the string that never changes!
Class name: class SomeClass Value: There you are

Process finished with exit code 0

According to documentation https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html

hasskell
  • 441
  • 3
  • 7
  • Have you seen [this](http://stackoverflow.com/questions/20945049/is-a-java-string-really-immutable) post? – Ravindra HV Mar 05 '16 at 09:27
  • This question asks about a `static` final field, so this code doesn't work. `setAccessible(true)` only works for setting final instance fields. – Radiodef Aug 02 '18 at 23:33
2

The accepted answer worked for me until deployed on JDK 1.8u91. Then I realized it failed at field.set(null, newValue); line when I had read the value via reflection before calling of setFinalStatic method.

Probably the read caused somehow different setup of Java reflection internals (namely sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl in failing case instead of sun.reflect.UnsafeStaticObjectFieldAccessorImpl in success case) but I didn't elaborate it further.

Since I needed to temporarily set new value based on old value and later set old value back, I changed signature little bit to provide computation function externally and also return old value:

public static <T> T assignFinalField(Object object, Class<?> clazz, String fieldName, UnaryOperator<T> newValueFunction) {
    Field f = null, ff = null;
    try {
        f = clazz.getDeclaredField(fieldName);
        final int oldM = f.getModifiers();
        final int newM = oldM & ~Modifier.FINAL;
        ff = Field.class.getDeclaredField("modifiers");
        ff.setAccessible(true);
        ff.setInt(f,newM);
        f.setAccessible(true);

        T result = (T)f.get(object);
        T newValue = newValueFunction.apply(result);

        f.set(object,newValue);
        ff.setInt(f,oldM);

        return result;
    } ...

However for general case this would not be sufficient.

Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
2

Many of the answers here are useful, but I've found none of them to work on Android, in particular. I'm even a pretty hefty user of Reflect by joor, and neither it nor apache's FieldUtils - both mentioned here in some of the answers, do the trick.

Problem with Android

The fundamental reason why this is so is because on Android there's no modifiers field in the Field class, which renders any suggestion involving this code (as in the marked answer), useless:

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

In fact, to quote from FieldUtils.removeFinalModifier():

// Do all JREs implement Field with a private ivar called "modifiers"?
final Field modifiersField = Field.class.getDeclaredField("modifiers");

So, the answer is no...

Solution

Pretty easy - instead of modifiers, the field name is accessFlags. This does the trick:

Field accessFlagsField = Field.class.getDeclaredField("accessFlags");
accessFlagsField.setAccessible(true);
accessFlagsField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

Side-note #1: this can work regardless of whether the field is static in the class, or not.

Side-note #2: Seeing that the field itself could be private, it's recommended to also enable access over the field itself, using field.setAccessible(true) (in addition to accessFlagsField.setAccessible(true).

d4vidi
  • 2,407
  • 26
  • 27
  • 1
    When running the original code in Android, you will get the following error: `java.lang.NoSuchFieldException: No field modifiers in class Ljava/lang/reflect/Field; (declaration of 'java.lang.reflect.Field' appears in /apex/com.android.runtime/javalib/core-oj.jar)`. The suggested solution works in my case. (This error currently has only one google result, so hopefully people will now find this page) – Dauntless Sep 23 '21 at 11:45
0

If your field is simply private you can do this:

MyClass myClass= new MyClass();
Field aField= myClass.getClass().getDeclaredField("someField");
aField.setAccessible(true);
aField.set(myClass, "newValueForAString");

and throw/handle NoSuchFieldException

Philip Rego
  • 552
  • 4
  • 20
  • 35
-6

The whole point of a final field is that it cannot be reassigned once set. The JVM uses this guarentee to maintain consistency in various places (eg inner classes referencing outer variables). So no. Being able to do so would break the JVM!

The solution is not to declare it final in the first place.

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • 6
    Moreover, `final` has a special role in multithreaded execution - changing `final` values would break the Java memory model too. – Péter Török Jul 21 '10 at 16:47
  • 2
    And a field not declared `final` should not be declared `static`. – Tom Hawtin - tackline Jul 21 '10 at 19:34
  • 2
    @Tom: In general that's probably true, but I wouldn't outlaw *all* static mutable variables. – bcat Jul 22 '10 at 12:37
  • @bcat It's almost always wrong. You might get away with it for a cache (or you might not). – Tom Hawtin - tackline Jul 22 '10 at 14:16
  • @Tom what about a Singleton using a `getInstance()` method that assigns the instance to a static field? You'll also need to reassign a mutable static field when it's a `WeakReference` and the original reference target has disappeared. – thecoop Jul 22 '10 at 16:49
  • 1
    @thecoop It is well known that singletons (a.k.a. globaltons) are evil. – Tom Hawtin - tackline Jul 22 '10 at 17:39
  • 7
    @Tom: Did you ever read up why singletons are evil? I did! Now I know that they evil only in Java. And only because of the availabilty of a user defined class loader. And ever since I know all this and I don´t use user defined class loader I use singletons without regret. And so does Scala where singleton are a first class language feature — That singletons are evil is is a well known **false myth**. – Martin Jun 10 '11 at 12:16
  • @Tom: I have 20 years of experience in Software development. I learned a dozen and more programming languages. **I researched the details about Singletons in Java**. So what make you think your are right? – Martin Jun 11 '11 at 14:54
  • 1
    The final modifier is not intended to protect a field from change. It just indicates, that the programmer of the class doesn't recommend changing this field. Same with accessors. They are only intended to show a user of a class what methods/fields are safe to use. – Vincent Dec 11 '12 at 23:03
  • @Vincent, isn't it contradict to the [@Péter Török's comment](http://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection#comment3420182_3301664)? – naXa stands with Ukraine Jan 13 '15 at 14:28
  • 4
    @Martin I know that your comment is old, and perhaps your views have changed by now, but I thought I'd just add this: Singletons are evil for reasons that have nothing to do with Java. They add ***hidden complexity*** to your code. Additionally, they can make it impossible to unit test without also knowing that *n* singletons must also be configured first. They are the antithesis of dependency injection. Your team might make the decision that the pitfalls of having *hidden complexity* don't outweigh the convenience of Singletons, but many teams take the opposite stance for good reason. – crush May 19 '16 at 16:00
  • No, it does not break the JVM. JVM allows `static final` fields to be modified by methods of the same class. – Some Name Aug 18 '19 at 21:09