Java反射操作成员变量 Class can not access a member with modifiers "*"
Posted zhjh256
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java反射操作成员变量 Class can not access a member with modifiers "*"相关的知识,希望对你有一定的参考价值。
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 "*"的主要内容,如果未能解决你的问题,请参考以下文章