设置私有静态最终字段
//package com.nowjava; /** 来自 N o w J a v a . c o m - 时 代 Java**/ import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static boolean setPrivateStaticFinalField(Class clazz, String fieldName, Object value) { return setPrivateFinalField(clazz, fieldName, null, value); } public static boolean setPrivateFinalField(Class clazz, String fieldName, Object instance, Object value) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Field modifiersField = Field.class .getDeclaredField("modifiers"); modifiersField.setAccessible(true); /** * 时代Java公众号 - nowjava.com 提 供 **/ modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(instance, value); return true; }