2011-04-26 62 views
1

的似乎不能夠編譯以下主要方法:無法編譯內部類的源文件,導致錯誤?

public class MainMethod { 

    public static void main(String []args){ 

     InnerizationClass outer = new InnerizationClass(); 
     InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1 
     System.out.println(inner.getOuterClassVar()); 
     System.out.println(new InnerizationClass().locInnrMeth()); 
    } 
} 

這是InnerizationClass類:

public class InnerizationClass { 

    String outerClassVar = "Outer Catism"; 
    static String outerClassVarStat = "Outer Static Catism"; 

    public static class StaticInnerClass{ 

     String innerClassVar = "Catism"; 

     public String getInnerClassVar(){ 

      return this.innerClassVar; 
     } 

     public String getOuterClassVar(){ 

      return InnerizationClass.outerClassVarStat; 
     } 
    } 

    public String locInnrMeth(){ 

     class MethodLocalInnerClass{ 

      String methodLocalInrCls = "Method Local Catism"; 

      void printInner(){ 

       System.out.println(InnerizationClass.this.outerClassVar); 
      } 

     } 

     return new MethodLocalInnerClass().methodLocalInrCls; 

    } 

    public String getStaticOuterVar(){return null;} 
} 

我從主方法獲取從#1以下錯誤:

MainMethod.java:6 qualified new of static class 
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass(); 
1 error; 

回答

5

試試這個:

public class MainMethod { 

public static void main(String []args){ 

    InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1 
    System.out.println(inner.getOuterClassVar()); 
    System.out.println(new InnerizationClass().locInnrMeth()); 
} 
} 

因爲你的內部類是靜態的,所以你不需要創建一個InnerizationClass的實例

相關問題