If print()
changes the value held in st
,
will this change the actual object?
No. Changing the reference will not change the object.
Here is an altered program in which the print()
method
changes the value held in its formal parameter.
class ObjectPrinter2 { public void print( String st ) { System.out.println("First value of parameter: " + st ); st = "Hah! A second Object!" ; System.out.println("Second value of parameter: " + st ); } } class OPTester2 { public static void main ( String[] args ) { String message = "Original Object" ; ObjectPrinter op = new ObjectPrinter(); System.out.println("First value of message: " + message ); op.print( message ); System.out.println("Second value of message: " + message ); } }
What is the output of the program?