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

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

報:spa

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);code

相關文章
相關標籤/搜索