Java examples for Reflection:Field Set
set Private Final Field
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static boolean setPrivateFinalField(Class clazz, String fieldName, Object instance, Object value) { try {/* w w w . j ava 2 s . c o m*/ Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Field modifiersField = Field.class .getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(instance, value); return true; } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return false; } }