Java變量的作用範圍

可以簡單的理解爲,一個變量的作用域是在所在的{}中。如果用同名變量,被覆蓋。

 

例子:

 

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();
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章