标题: 关于反射:私有变量设置为不可访问时依然有访问到... [打印本页] 作者: YongBao_Summer 时间: 2015-6-7 20:37 标题: 关于反射:私有变量设置为不可访问时依然有访问到... 设置为私有的变量,并且设置为不可到达,为什么在我的电脑上显示出现结果。
有兴趣的同学拿去试验一下,看看是否与我电脑配置有关系。
我用的是jdk 1.7版本。
public class InstanceDemo {
public int x;
private int y;
public InstanceDemo(int x,int y) {
super();
this.x = x;
this.y = y;
}
public static void main(String[] args)throws Exception{
// Field y = InstanceDemo.class.getField("x");
// Field x = InstanceDemo.class.getDeclaredField("y");
InstanceDemo i = new InstanceDemo(5,10);
Field fieldY = i.getClass().getDeclaredField("y");
fieldY.setAccessible(false);
System.out.println(fieldY.get(i));
}
}