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
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.
Could print()
use the reference to the
original String object to change the contents of that object?