CtrlAltDelete CtrlAltDelete - 1 month ago 13
Java Question

How to define a Constructor that takes zero arguments, and initializes its field to itself?

I'm teaching myself Java and I came across a confusing part. It states

"For Sentinel<T>, define a constructor that takes zero arguments, and initializes the next and prev fields of the Sentinel to the Sentinel itself."


below is the class structure I have created for the problem. I can't wrap my head around what it's asking for. I tried
this.next;
and
this.prev;
but it throws an error.

class Deque<T> {
Sentinel<T> header;
}

class Sentinel<T> extends ANode<T>{
Sentinel<T> next;
Sentinel<T> prev;

Sentinel() {
//What goes here?????????
}
}

class ANode<T> {
ANode<T> next;
ANode<T> prev;
}

class Node<T> extends ANode<T>{
T data;
}

Answer
Sentinel() {
    //What goes here?????????
}

should be:

Sentinel() {
    //What goes here?????????
    this.next = this;
    this.prev = this;
}

What it does is:

For Sentinel, a constructor that takes zero arguments, and initializes the next and prev fields of the Sentinel to the Sentinel itself.