Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming no <code>SecurityManager</code> is preventing you from doing this, you can use <code>setAccessible</code> to get around <code>private</code> and resetting the modifier to get rid of <code>final</code>, and actually modify a <code>private static final</code> field.</p> <p>Here's an example:</p> <pre><code>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() &amp; ~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" } } </code></pre> <p>Assuming no <code>SecurityException</code> is thrown, the above code prints <code>"Everything is true"</code>.</p> <p>What's actually done here is as follows:</p> <ul> <li>The primitive <code>boolean</code> values <code>true</code> and <code>false</code> in <code>main</code> are autoboxed to reference type <code>Boolean</code> "constants" <code>Boolean.TRUE</code> and <code>Boolean.FALSE</code></li> <li>Reflection is used to change the <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/Boolean.html#FALSE" rel="noreferrer"><code>public static final Boolean.FALSE</code></a> to refer to the <code>Boolean</code> referred to by <code>Boolean.TRUE</code></li> <li>As a result, subsequently whenever a <code>false</code> is autoboxed to <code>Boolean.FALSE</code>, it refers to the same <code>Boolean</code> as the one refered to by <code>Boolean.TRUE</code></li> <li>Everything that was <code>"false"</code> now is <code>"true"</code></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2474017/using-reflection-to-change-static-final-file-separatorchar-for-unit-testing/2474242#2474242">Using reflection to change <code>static final File.separatorChar</code> for unit testing</a></li> <li><a href="https://stackoverflow.com/questions/2481862/how-to-limit-setaccessible-to-only-legitimate-uses">How to limit setAccessible to only “legitimate” uses?</a> <ul> <li>Has examples of messing with <code>Integer</code>'s cache, mutating a <code>String</code>, etc</li> </ul></li> </ul> <hr> <h3>Caveats</h3> <p>Extreme care should be taken whenever you do something like this. It may not work because a <code>SecurityManager</code> may be present, but even if it doesn't, depending on usage pattern, it may or may not work.</p> <blockquote> <p><a href="http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5.3" rel="noreferrer">JLS 17.5.3 Subsequent Modification of Final Fields</a></p> <p>In some cases, such as deserialization, the system will need to change the <code>final</code> fields of an object after construction. <code>final</code> 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 <code>final</code> fields of the object are updated. The object should not be made visible to other threads, nor should the <code>final</code> fields be read, until all updates to the <code>final</code> fields of the object are complete. Freezes of a <code>final</code> field occur both at the end of the constructor in which the <code>final</code> field is set, and immediately after each modification of a <code>final</code> field via reflection or other special mechanism.</p> <p>Even then, there are a number of complications. If a <code>final</code> field is initialized to a compile-time constant in the field declaration, changes to the <code>final</code> field may not be observed, since uses of that <code>final</code> field are replaced at compile time with the compile-time constant.</p> <p>Another problem is that the specification allows aggressive optimization of <code>final</code> fields. Within a thread, it is permissible to reorder reads of a <code>final</code> field with those modifications of a final field that do not take place in the constructor.</p> </blockquote> <h3>See also</h3> <ul> <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28" rel="noreferrer">JLS 15.28 Constant Expression</a> <ul> <li>It's unlikely that this technique works with a primitive <code>private static final boolean</code>, because it's inlineable as a compile-time constant and thus the "new" value may not be observable</li> </ul></li> </ul> <hr> <h3>Appendix: On the bitwise manipulation</h3> <p>Essentially,</p> <pre><code>field.getModifiers() &amp; ~Modifier.FINAL </code></pre> <p>turns off the bit corresponding to <code>Modifier.FINAL</code> from <code>field.getModifiers()</code>. <code>&amp;</code> is the bitwise-and, and <code>~</code> is the bitwise-complement.</p> <h3>See also</h3> <ul> <li><a href="http://en.wikipedia.org/wiki/Bitwise_operations" rel="noreferrer">Wikipedia/Bitwise operation</a></li> </ul> <hr> <h3>Remember Constant Expressions</h3> <p>Still not being able to solve this?, have fallen onto depression like I did for it? Does your code looks like this?</p> <pre><code>public class A { private final String myVar = "Some Value"; } </code></pre> <p>Reading the comments on this answer, specially the one by @Pshemo, it reminded me that <a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28" rel="noreferrer">Constant Expressions</a> are handled different so it will be <strong>impossible</strong> to modify it. Hence you will need to change your code to look like this:</p> <pre><code>public class A { private final String myVar; private A() { myVar = "Some Value"; } } </code></pre> <p><em>if you are not the owner of the class... I feel you!</em></p> <p>For more details about why this behavior <a href="https://stackoverflow.com/questions/17506329/java-final-field-compile-time-constant-expression">read this</a>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload