本帖最后由 chenlanaq 于 2015-3-29 16:55 编辑
将输出去掉注释,就会出异常。。
- class clazz {
- public static final Integer FINALOBJ=12345;
- }
- class MString {
- public static void main(String[] args)throws Exception{
- Field field=null;
- field = clazz.class.getDeclaredField("FINALOBJ");
- field.setAccessible(true);
- // System.out.println(field.get(null));
- setFinal(field,67890);
- field.setAccessible(true);
- System.out.println(field.get(null));
- }
- static void setFinal(Field field, Object newValue) throws Exception {
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(field, newValue);
- }
- }
复制代码
|
|