Node next;
Is what you could call a forward reference. It tells you that an instance of that class Node
has a reference that can point to another Node instance.
Initially of course, that reference will be null (not pointing to another Node
). But when you start building your list of nodes, that works by one Node ... linking to the next
Node, by changing that field value from null, to point to some instance of that class. That is how simple linked lists work: you start with a single root node, and adding notes to the list means: updating such next
fields.
From that point of view, there is no "real" difference between the two classes above. You could as well go:
Node firstNode = new Node(5);
Node secondNode = new Node(42);
firstNode.next = secondNode;
The main point of having a distinct class with a first
field is for conceptual reasons: that second class "tells" the reader: "my purpose is to contain the first Node object of a list".
You can turn here for more extensive explanations about classes, objects, and references.
And note: the really crucial point here is that, by default, when you do a new Node()
call, that field next
starts of with null
.