A parameter is a name used in a method definition for values that will be passed into the method by its callers.
Here is an example
of a method that uses a parameter.
(The example is from the CheckingAccount
class of the last two chapters.)
The ellipses (. . . .)
show where code not relevant to this
discussion has been left out.
class CheckingAccount { . . . . private int balance; . . . . void processDeposit( int amount ) { balance = balance + amount ; } }
The parameter amount
is used by a caller send a
value to the method.
This is usually called passing a value
into the method.
Here is part of a main()
method that uses the parameter to
pass a value into the processDeposit()
method:
class CheckingAccountTester
{
public static void main( String[] args )
{
CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
bobsAccount.processDeposit( 200 );
// . . . . assume that more statements follow
}
}
When the statement
bobsAccount.processDeposit( 200 );
is executed,
the parameter amount
of the object's method will hold
the value 200.
This value is added to the object's instance variable in the statement
balance = balance + amount ;
Then the method will be exited and control will return to main()
.
The state of the object referred to by bobsAccount
will have
been changed.