今天看到一段定义Node的代码,发现Node类中的成员变量是Node的对象,如下。
public class Node {
private final int value;
private Node next;
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
复制代码
以上的代码第三行Node对象有点像递归,注意该对象不能初始化,或者只有满足某些条件的时候,才能初始化(可以给静态的自身对象赋值)否则会导致内存溢出。
参考: