我试图学习反思,但我提出了一个问题,为什么没有例外?
public class FieldExceptionTest {
private boolean b = true;
public static void main(String[] args) throws Exception{
FieldExceptionTest ft = new FieldExceptionTest();
Class<?> c = ft.getClass();
Field f = c.getDeclaredField("b");
// f.setAccessible(true); //if i don't write this line, it also can run.
f.setBoolean(ft, Boolean.FALSE);
System.out.println(ft.b);
}
}
为什么它没有抛出 IllegalAccessException?通过阅读其他书籍,我知道如果尝试获取或设置私有或其他不可访问字段的值或设置最终字段的值,可能会引发 IllegalAccessException。但是在这里,它没有,为什么?