看代碼:

public class Test {

    public int aMethod(){
        static int i = 0;   //  編譯失敗,  靜態變量只能在類主體中定義,不能在方法中定義
        i++;
        return i;
    }

    public static void main(String args[]){
        Test test = new Test();
        test.aMethod();
        int j = test.aMethod();
        System.out.println(j);
    }
}

相關文章
相關標籤/搜索