views:

634

answers:

3

I have a class with a private static final field, that unfortunately i need to change at run time.

using reflection i get this error: java.lang.IllegalAccessException: Can not set static final boolean field

is there any possibility to change it anyway?

Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK");
hack.setAccessible(true);
hack.set(null, true);
+2  A: 

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
Moreover, `final` has a special role in multithreaded execution - changing `final` values would break the Java memory model too.
Péter Török
And a field not declared `final` should not be declared `static`.
Tom Hawtin - tackline
@Tom: In general that's probably true, but I wouldn't outlaw *all* static mutable variables.
bcat
@bcat It's almost always wrong. You might get away with it for a cache (or you might not).
Tom Hawtin - tackline
@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<T>` and the original reference target has disappeared.
thecoop
@thecoop It is well known that singletons (a.k.a. globaltons) are evil.
Tom Hawtin - tackline
+9  A: 

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

polygenelubricants
Extra points for evilness...
thecoop
This works with `separatorChar` because it is assigned from a runtime expression. It's not a general solution.
erickson
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
@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
+4  A: 

If the value assigned to a static final boolean field is known at compile-time, it is a constant. Its value will be inlined in any code that references the value. Since it's 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
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
@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
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
@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