/**
* 就近原则
*
* 之所以在不同作用域定义相同变量名不会出错的原因是因为JVM在调用该变量时,
* 查找顺序是:先找同一作用域的变量,再找上一级作用域的变量,直到找到一个为止。
* 所以,像在下面代码里面那样命名,并不会让JVM分辨不清。
*
*
*/
public class MainTest {
static int num = 4;
public static void test(){
int num = 5;
System.out.println(num);
}
public static void main(String[] args) {
test();
}
}
结果:5
754

被折叠的 条评论
为什么被折叠?



