静态方法调用非静态方法——编译出现错误

 

出现:No enclosing instance of type Test_Static is accessible. Must qualify the allocation with an enclosing instance of type Test_Static (e.g. x.new A() where x is an instance of Test_Static).

上面的编译错误:可能由于静态public static main调用类的非静态方法AA

有两种解决办法:

第一种:

将内部类AA定义成静态static的类。

第二种:

将内部类AA在Main类外边定义。

 1、

public class Test_Static {
static class AA{
    public void print(){
        System.out.println("调用类方法");
    }
}
public static void main(String[] args) {
 AA aa=new AA();
 aa.print();
}

}

2、

class AAA{
    public void print(){
        System.out.println("调用类方法");
    }
}
public class Test_Static {
public static void main(String[] args) {
 AAA aa=new AAA();
 aa.print();
}

}

 

posted @ 2016-11-20 10:48  静若飘絮  阅读(500)  评论(0编辑  收藏  举报