Posted By

joycollector on 09/26/11


Tagged

final field private static


Versions (?)

Change private static final field using java reflection


 / Published in: Java
 

URL: http://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection

  1. import java.lang.reflect.*;
  2.  
  3. public class EverythingIsTrue {
  4. static void setFinalStatic(Field field, Object newValue) throws Exception {
  5. field.setAccessible(true);
  6.  
  7. Field modifiersField = Field.class.getDeclaredField("modifiers");
  8. modifiersField.setAccessible(true);
  9. modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  10.  
  11. field.set(null, newValue);
  12. }
  13. public static void main(String args[]) throws Exception {
  14. setFinalStatic(Boolean.class.getField("FALSE"), true);
  15.  
  16. System.out.format("Everything is %s", false); // "Everything is true"
  17. }
  18. }

Report this snippet  

You need to login to post a comment.