No enclosing instance of type B is accessible. Must qualify the allocation with an enclosing ins (20
2012-08-07 12:54
585 查看
编译时出现:No enclosing instance of type B is accessible. Must qualify the allocation with an enclosing instance of type B(e.g. x.new B() where x is an instance of B).
B指代内部类。
根据提示,没有可访问的内部类B的实例,必须分配一个合适的内部类B的实例(如x.new B(),x必须是B的实例)
原来内部类B是动态的,也就是以class开头。
在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。
所以解决办法是将class改为static class.
下文摘录网上描述的一段话:
如果一个成员变量被声明为static,它就能够在它的类的任何对象创建之前被访问,而不必引用任何对象。
你可以将方法和变量都声明为static。static成员最常见的例子是main()。因为在程序开始执行时必须调用main(),所以它被声明为static。
下面是例子:
或者:
B指代内部类。
根据提示,没有可访问的内部类B的实例,必须分配一个合适的内部类B的实例(如x.new B(),x必须是B的实例)
原来内部类B是动态的,也就是以class开头。
在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。
所以解决办法是将class改为static class.
下文摘录网上描述的一段话:
如果一个成员变量被声明为static,它就能够在它的类的任何对象创建之前被访问,而不必引用任何对象。
你可以将方法和变量都声明为static。static成员最常见的例子是main()。因为在程序开始执行时必须调用main(),所以它被声明为static。
下面是例子:
public class A { public static void main(String[] args) { new B().print(); } static class B { //必须用static public void print() { System.out.println("Hello, world!"); } } }
或者:
public class A { public static void main(String[] args) { A a = new A(); //必须先建立一个对象 a.new B().print(); } class B { public void print() { System.out.println("Hello, world!"); } } }
相关文章推荐
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing ins
- 出现No enclosing instance of type Exp is accessible. Must qualify the allocation with an enclosing ins
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java 程序报错“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclo”
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type FactoryModel is accessible. Must qualify the allocation with an enclos
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- No enclosing instance of type E is accessible. Must qualify the allocation with an en
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosin的解决方案
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
- No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instanc
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)