[問題]請問JAVA的方法是不是不能用static int

看板java作者 (BMW 428i)時間4年前 (2020/01/20 13:29), 編輯推噓3(304)
留言7則, 6人參與, 4年前最新討論串1/1
請問一下 public static int showString(ArrayList<Integer> arrayList) { static int counter=0; System.out.println("_________________________________________________"); System.out.println("arraysize is="+arrayList.size()); char[] CH = new char[arrayList.size()]; for (int loop = 0; loop < arrayList.size(); loop++) { CH[loop] = byteAsciiToChar(arrayList.get(loop)); } 請問我在JAVA的方法裡面設定一個靜態變數static int counter 為什麼顯示語法錯誤??? 而且我在Main()裡面寫static counter=0一樣不可以??WHY?? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.246.156.168 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/java/M.1579498182.A.5D6.html

01/20 13:47, 4年前 , 1F
static 變數請在 class 層級宣告
01/20 13:47, 1F

01/20 14:05, 4年前 , 2F
你在方法內宣告靜態是很奇怪的事情
01/20 14:05, 2F

01/20 16:10, 4年前 , 3F
本地變數可以用static嗎
01/20 16:10, 3F

01/20 17:15, 4年前 , 4F
java沒有C/C++的那種static
01/20 17:15, 4F

01/20 17:16, 4年前 , 5F
只有class層級有static member
01/20 17:16, 5F

01/22 23:43, 4年前 , 6F
不是阿!你的method 已經是static了啊!
01/22 23:43, 6F

01/23 13:34, 4年前 , 7F
你已經宣告這個方就是靜態,裡面的不用再宣告了
01/23 13:34, 7F
文章代碼(AID): #1U9Jh6NM (java)