Return to Snippet

Revision: 51478
at September 26, 2011 14:45 by joycollector


Initial 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() & ~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"
   }
}

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

Initial Description

                                

Initial Title
Change private static final field using java reflection

Initial Tags

                                

Initial Language
Java