First value of the local var: 7 First value of the parameter: 7 Second value of the parameter: 100 Second value of the local var: 7
Once a value has been copied into a parameter a method can use the value or change the value. But a change to a parameter does not affect any variable inside the caller. How can a method send a value back to the caller? Examine the following:
To return a single value to the caller,
an invoked method can use the return
statement
along with the value to be returned.
class SimpleClassTwo
{
public int twice( int x )
{
return 2*x;
}
}
class SimpleTesterTwo
{
public static void main ( String[] args )
{
int var = 7;
int result = 0;
SimpleClassTwo simple = new SimpleClassTwo();
System.out.println("First value of result: " + result );
result = simple.twice( var );
System.out.println("Second value of result: " + result );
}
}
Now what is the output of the program?