go to previous page   go to home page   go to next page

Answer:

A parameter is a value that is sent to a method when it is called.


Parameters

Here is an example of a method that uses a parameter.


class CheckingAccount
{
  . . . .
  private int balance;

  . . . .
  public void processDeposit( int amount )
  {
    balance = balance + amount ; 
  }

}

The parameter amount is used by a caller to send a value to the method. This is 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 of an object:


class CheckingAccountTester
{
  public static void main( String[] args )
  {
    CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
    
    bobsAccount.processDeposit( 200 );

    . . . . . .
    
  }
}

When the statement

bobsAccount.processDeposit( 200 );

is executed, the parameter amount is given the value 200. Now, as the processDeposit method executes, that value is added to the object's instance variable balance:

balance = balance + amount ; 

Then the method finishes and control returns to main(). The balance of the bobsAccount object has been changed.


QUESTION 2:

  1. Will the instance variable balance of the object hold its value permanently?
  2. Will the parameter amount of the object's method hold its value permanently?