无法编译内部类源文件,导致错误?

时间:2011-04-26 10:52:34

标签: java compiler-errors nested-class inner-classes

似乎无法编译以下主要方法:

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;

1 个答案:

答案 0 :(得分: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的实例