1 回答

TA贡献1802条经验 获得超5个赞
您在外部类本身内部使用静态类,因此您不要放置封闭类名称。静态嵌套类在行为上类似于任何静态字段。
但是如果要在外部类之外实例化静态嵌套类,则必须在其定义上放置封闭类名称或使用对外部类的引用。
例如 :
public class Main {
static class NodeInside {
int data;
NodeX.Node next;
NodeInside(int data) {
this.data = data;
next = null;
}
}
public static void main(String[] args) {
NodeX ll = new NodeX();
NodeX.Node head = new NodeX.Node(1); // need to put the enclosing class name
NodeInside nodeInside = new NodeInside(1); // no need to put the enclosing class
}
}
class NodeX{
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
}
添加回答
举报