[問題] 新手 static inner class問題

看板java作者 (You Are The One!!)時間18年前 (2008/03/22 23:30), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
class MyOuter { static class MyInner{ public void fooA(){ System.out.println("this is non-static method fooA"); } public static void fooB(){ System.out.println("this is static method fooB"); } } } public class Test { public static void main(String args[]) { MyOuter.MyInner m = new MyOuter.MyInner(); m.fooA(); m.fooB(); MyOuter.MyInner().fooB(); } } 以上是照書(tiger)上打的 但是compile錯誤 倒數第三行應該是new MyOuter.MyInner().fooB(); 對嗎? 因為書上還特地說不需要透過new來建立相關物件實體 這是我自己認為的啦 不過不太相信自己 所以來版上問問看 感謝~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.216.47.98

03/23 02:04, , 1F
應該是這下面這行的問題…
03/23 02:04, 1F

03/23 02:05, , 2F
MyOuter.MyInner().fooB(); //MyInner不加()試試
03/23 02:05, 2F

03/23 13:46, , 3F
感謝 我懂了 忘了MyInner是static
03/23 13:46, 3F
文章代碼(AID): #17vIOjlC (java)