First value of the local var: 7 Value of the parameter: 7 Second value of the local var: 7
Here is the program again, with a slight change.
Now the invoked method
print()
makes a change to its copy of the value
in its formal parameter x
.
class SimpleClass { public void print( int x ) { System.out.println("First value of the parameter: " + x ); x = 100; // local change to the formal parameter System.out.println("Second value of the parameter: " + x ); } } class SimpleTester { public static void main ( String[] args ) { int var = 7; SimpleClass simple = new SimpleClass(); System.out.println("First value of the local var: " + var ); simple.print( var ); System.out.println("Second value of the local var: " + var ); } }
Recall that this is using call by value which means parameters are used to pass values into a method, but not used to pass anything back to the caller.
Now what is the output of the program?