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

Answer:

First  value of message: Original Object 
First value of parameter: Original Object 
Second value of parameter: Hah! A second Object! 
Second value of message:   Original Object 

Revised Picture

In the revised program, the print() method creates a new object and places a reference to it into the parameter st. This is OK as long as the method no longer needs access to the first object.

Note that the second object becomes garbage when the method returns to the caller.

Each color of line in the picture represents a different reference value. The picture shows the print() method changing the value in the formal parameter so that it refers to a second object.

Neither the original object, nor the variable message of the main() method are changed. The print() method can use its formal parameter just like any variable, including changing the value it holds.

two objects

QUESTION 9:

Could print() use the reference to the original String object to change the contents of that object?