First value of the result: 0 Second value of the result: 14
Object references can be parameters. Call by value is used, but now the value is an object reference. This reference can be used to access the object and possibly change it. Here is an example program:
In this program, the local variable message
contains a reference to a String
object.
That reference is the value that is copied into the formal parameter st
of the print()
method.
The object itself is not copied.
class ObjectPrinter { public void print( String st ) { System.out.println("Value of parameter: " + st ); } } class OPTester { public static void main ( String[] args ) { String message = "Only One 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?