[英]Why can I access the private variable in this case?
我有一个私有int变量,我可以通过test.a访问该变量。 为什么这样可以? 我认为只有当我将public int设置为变量而不是私有时,这种方式才可行。
public class Test {
private int a;
public static void main(String[] args) {
Test test = new Test(5);
System.out.println(test.a);
}
public Test (int a) {
this.a = a;
}
}
您可以访问它,因为您的main
方法在同一类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.