1. 程式人生 > >Java反射操作成員變量 Class can not access a member with modifiers "*"

Java反射操作成員變量 Class can not access a member with modifiers "*"

leo acc ember ava get ssi slow private java反射操作

fields[j].set(obj, val);

報:

Exception in thread "main" java.lang.IllegalAccessException: Class pers.hanchao.reflect.fields.ReflectFieldDemo can not access a member of class pers.hanchao.reflect.common.User with modifiers "private"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:
102) at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296) at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288) at java.lang.reflect.Field.get(Field.java:390) at

解決方案:添加一行代碼:fields[j].setAccessible(true);

Java反射操作成員變量 Class can not access a member with modifiers "*"