我用下面的代码段,这给了我一个错误的点指示:静态嵌套类与非静态错误?
class LinkedList{
class pair{
Integer petrol;
Integer distance;
public pair (Integer a, Integer b){
petrol = a;
distance = b;
}
}
public static void main(String args[]){
pair[] circle = {new pair(4,6), new pair(6,5), new pair(7,3), new pair(4,5)}; // error at first element of array circle!!!!!!!
}
}
我那么纠正这个和错误dissapeared!
class LinkedList{
static class pair{ // changed to static!!!
Integer petrol;
Integer distance;
public pair (Integer a, Integer b){
petrol = a;
distance = b;
}
}
public static void main(String args[]){
pair[] circle = {new pair(4,6), new pair(6,5), new pair(7,3), new pair(4,5)}; //error gone!
}
}
我的问题是为什么错误甚至出现在第一位呢?
ERROR: No enclosing instance of type LinkedList is accessible. Must qualify the allocation with an enclosing instance of type LinkedList.
如果没有static关键字,pair就会变成LinkedList的内部类,这意味着每个'pair'对象都必须和封闭'LinkedList'类的实例关联。 – Eran