class Test{
private static int i = 1;
public static void main(String[] args){
Test test = new Test();
System.out.println(test.i); //此处为何能访问到私有的i变量呢?
}
}
Theoretically, access modifiers are not completely necessary and will not affect program logic. This is only during the compilation phase to help programmers write safer code. I understand the confusion of the questioner. He thinks it is more appropriate to use this.i here (of course, remove the crappy static keyword). This view may be true. It can make some people go crazy. this.i较为妥当(当然,去掉那个蹩脚的static关键字吧),这个观点可能还真会使一些人走火入魔。 记住:这些访问修饰符与执行上下文无关,比如privateRemember: These access modifiers have nothing to do with the execution context. For example, private will only check whether the code you use to access this variable is written in the current class at compile time. It is as simple and crude as that.
Theoretically, access modifiers are not completely necessary and will not affect program logic. This is only during the compilation phase to help programmers write safer code.
I understand the confusion of the questioner. He thinks it is more appropriate to use
this.i
here (of course, remove the crappystatic
keyword). This view may be true. It can make some people go crazy.this.i
较为妥当(当然,去掉那个蹩脚的static
关键字吧),这个观点可能还真会使一些人走火入魔。记住:这些访问修饰符与执行上下文无关,比如
private
Remember: These access modifiers have nothing to do with the execution context. For example,private
will only check whether the code you use to access this variable is written in the current class at compile time. It is as simple and crude as that.