Is this OK? Sure. The caller has sent the value 7000 to the method.
What does amount = 0
do in the method?
This changes the local copy of the value from 7000 to 0.
But this does not affect the caller. Look back at the caller: the value 7000 is not contained in a variable. There is nothing that the called method could change.
A local variable is a variable that is declared inside of the body of a method. It can be seen only by the statements that follow its declaration inside of that method.
The scope of a local variable
starts where it is declared and ends at the
end of the block that it is in.
(Recall that a block is a group of statements inside of
braces, {}
.)
For example, charge
of processCheck
is a local variable:
class CheckingAccount { . . . . private int balance; void processCheck( int amount ) { int charge; // scope of charge starts here incrementUse(); if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // scope of charge ends here } }
The local variable charge
is used
to hold a temporary value while something is being
calculated.
Local variables are not used to hold the permanent values of
an object's state.
They have a value only during the brief amount of time that a
method is active.
Is the scope of a local variable always the entire body of a method?