再学Java 之 解决No enclosing instance of type * is accessible
2014-04-20 02:29
351 查看
深夜,临睡前写了个小程序,出了点小问题
上面两个语句报错信息如下:
(1)在stackoverflow上面查找到了类似的问题:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633
(2)下面简单说一下我的理解:
在这里,A和B都是Test_drive的内部类,类似于普通的实例变量,如果类的静态方法不可以直接调用类的实例变量。在这里,内部类不是静态的内部类,所以,直接赋值(即实例化内部类),所以程序报错。
(3)解决的方法可以有以下两种:
(1)将内部类定义为static,即为静态类
(2)将A a = new A();B b = new B();改为:
附注:写到这里好困。如果大家有更好的理解,请在下面留言。谢谢。
public class Test_drive { public static void main(String[] args){ A a = new A(); //报错 B b = new B(); //报错 System.out.println(b instanceof A); } class A{ int a; } class B extends A{ } }
上面两个语句报错信息如下:
No enclosing instance of type Test_drive is accessible. Must qualify the allocation with an enclosing instance of type Test_drive (e.g. x.new A() where x is an instance of Test_drive).
(1)在stackoverflow上面查找到了类似的问题:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633
(2)下面简单说一下我的理解:
在这里,A和B都是Test_drive的内部类,类似于普通的实例变量,如果类的静态方法不可以直接调用类的实例变量。在这里,内部类不是静态的内部类,所以,直接赋值(即实例化内部类),所以程序报错。
(3)解决的方法可以有以下两种:
(1)将内部类定义为static,即为静态类
(2)将A a = new A();B b = new B();改为:
Test_drive td = new Test_drive(); A a = td.new A(); B b = td.new B();
附注:写到这里好困。如果大家有更好的理解,请在下面留言。谢谢。
相关文章推荐
- Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案
- Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题
- Java出现No enclosing instance of type E is accessible问题解决
- JAVA编译错误 “No enclosing instance of type XXX is accessible”的解决方法
- java解决 No enclosing instance of type XXX is accessible的问题
- java中"no enclosing instance of type * is accessible"的解决方法
- 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
- 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
- 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 ThreadTest is accessible.错误解决
- 当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
- Java出现No enclosing instance of type H is accessible. Must qualify the allocation with an enclosing
- Java中出现No enclosing instance of type XXX is accessible问题
- Java出现No enclosing instance of type E is accessible 错误
- 【Java-bug】No enclosing instance of type Test is accessible. Must qualify the allocation with an encl
- Java在编译运行时出现No enclosing instance of type XXX is accessible问题