P_K P_K - 1 year ago 150
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 Source

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(.....); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download