P_K P_K - 10 months ago 80
Java Question

Object Declaration inside classes

class Node
Node next;
int data;

As in this above example for creating a node in a link list, why did we not use "new" keyword to declare the node object inside the node class?


new is not used for declaration but for creating an object.

The code

 Node next; 

is a declaration only.

To create a Node object you will need to use one of Node's constructors and do something like:

 next = new Node(.....);