Java出現No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

  原文地址:http://blog.csdn.net/sunny2038/article/details/6926079 ide


  最近在看Java,在編譯寫書上一個例子時,因爲書上的代碼只有一部分,因而就本身補了一個內部類。結果編譯時出現:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我寫的那個內部類。.net


  根據提示,沒有可訪問的內部類E的實例,必須分配一個合適的內部類E的實例(如x.new A(),x必須是E的實例。)看着這句提示,我就納悶了,我已經用new實例化了這個類,爲何還不行呢。blog


  因而百度谷歌了一下相關資料。原來我寫的內部類是動態的,也就是開頭以public class開頭。而主程序是public static class main。在Java中,類中的靜態方法不能直接調用動態方法。只有將某個內部類修飾爲靜態類,而後纔可以在靜態類中調用該類的成員變量與成員方法。因此在不作其餘變更的狀況下,最簡單的解決辦法是將public class改成public static class.get

相關文章
相關標籤/搜索