Change private static final field using java reflection


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.