eclipse报错:
No enclosing instance of type UserTest is accessible. Must qualify the allocation with an enclosing instance of type UserTest (e.g. x.new A() where x is an instance of UserTest).
我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。
public class UserTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
User u = new User();//此处报错
}
class User{
String name;
int age=1;
}
}
只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。
方法一:将public class改为public static class.
方法二:没有必要把A放在Test中做内部类,只要把它移到Test外部地方定义也可以解决这个问题。