可以簡單的理解爲,一個變量的作用域是在所在的{}中。如果用同名變量,被覆蓋。
例子:
public class TestVariableScope {
int i = 1;
void test(){
int i = 2;
System.out.println(i);
}
public static void main(String[] args) {
TestVariableScope variableScope = new TestVariableScope();
System.out.println("成員變量: " + variableScope.i);
System.out.print("局部變量: ");
variableScope.test();
}
}