| 特点 | 描述 |
|---|---|
| 作用范围 | 离当前变量最近的大括号({})以内 |
| 重名问题 | 重合的作用范围不能重名 |
| 定义位置 | 方法中 |
if (a>10) {
int c = 20;
System.out.println(c);
}else {
int c=30;
System.out.println(c);
}
此处c的作用范围未重合,此处程序成立
public static void main(String[] args) {
int a = 20;
if (a>10) {
int a = 2;
}
}
此处a = 20;的作用范围包括了a = 2;的作用范围,此处程序不成立



