P_K P_K - 4 months ago 18
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?

Answer

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(.....); 
Comments