這是我的主類:如何填寫內部班的Arraylist?
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args){
ArrayList<SecondClass.InnerClass> list=new ArrayList<SecondClass.InnerClass>();
list.add(new SecondClass.InnerClass()); //error here (read below)
}
}
這裏是第二類:
public class SecondClass {
public class InnerClass{
}
}
在MainClass
,在list.add
,我得到這個錯誤:
No enclosing instance of type SecondClass is accessible. Must qualify the allocation with an enclosing instance of type SecondClass (e.g. x.new A() where x is an instance of SecondClass).
我需要有InnerClass
非靜態,因爲InnerClass
需要對非靜態方法進行靜態引用。我如何在ArrayList
中添加元素?
申報'InnerClass'爲'static' – 2014-10-10 16:05:46
@ColonelThirtyTwo不會是一個內部類,但一個嵌套類。 – 2014-10-10 16:07:53
什麼是「對靜態方法的靜態引用」? – 2014-10-10 16:08:06