go to previous page   go to home page   go to next page highlighting

Answer:

Yes. All objects are accessed through a reference, and node0.next could contain such a reference.


Node Pointing to Node

Here is another program:

public class NodeTester
{
  public static void main ( String[] args )
  {
    Node node0 = new Node( 223 );  
    Node node1 = new Node( 493 );
    System.out.println("Node 0: " + node0 + ", Node 1: " + node1 );  
      
    node0.setNext( node1 );
    System.out.println("Node 0: " + node0 + ", Node 1: " + node0.getNext() );     
  }
}

Now there are two nodes. The next member of node0 has been set to contain a reference to node1. In other words,

node1

and

node0.getNext()

are identical references to the same object. Here is a picture:

two linked nodes picture

QUESTION 5:

What is printed by these statements from the program?

System.out.println("Node 0: " + node0 + ", Node 1: " + node1 );  

node0.setNext( node1 );
System.out.println("Node 0: " + node0 + ", Node 1: " + node0.getNext() );   

(Recall that the toString() method is automatically invoked.)


go to previous page   go to home page   go to next page