最近在写代码时,遇到一个错误提示,老是不知道为什么,于是google了一下,将结果写下来,供大家学习和交流。
错误提示:
No enclosing instance of type Foo is accessible. Must qualify the allocation with an enclosing instance of type Foo (e.g. x.new A() where x is an instance of Foo).
原因是:
if you try to instantiate an inner class without an instance of the outer class.
在没有外部类对象时试图初始化一个内部类。
所以你应该先new一个外部类,然后通过它来new内部类
参见:
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
http://java.syntaxerrors.info/index.php?title=No_enclosing_instance_of_type_Foo_is_accessible._Must_qualify_the_allocation_with_an_enclosing_instance_of_type_Foo_(e.g._x.new_A()_where_x_is_an_instance_of_Foo).