Re: [問題] SCJP練習題
※ 引述《FatDevil (You Are The One!!)》之銘言:
: 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);
: }
: }
: 請問一下是錯在哪阿?
: 我看解答是i一定要是final(當然沒有i++)這樣就可以了,why?
: 懇請各位高手指導
: 感謝~~
aMethod()裡面的i是區域變數 區域變數是不能加上static的唷
把static拿掉就可以了 i++還可以存在
至於final 的話 i++當然不能存在 因為宣成final的話i就不允許更變了
加上i++的話就違反java的final規則了
如有錯誤請各位大大指正
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.127.52.105
討論串 (同標題文章)