Scope of a Formal Parameter

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

No. The formal parameter amount belongs to the processDeposit method. It cannot be used by any other method.

Scope of a Formal Parameter

The scope of a formal parameter is the section of code that can "see" the parameter. The scope of a formal parameter is the body of its method. For example, the scope of amount is the body of its method:

class CheckingAccount
{
  . . . .
  private int balance;

  . . . .
  public void processDeposit( int amount )
  { // scope of amount starts here
  
    balance = balance + amount ;   
       
    // scope of amount ends here
  }

  // modified toString() method
  public String toString()
  {
    System.out.println( balance + "/t" + amount );
  }


}

The toString() method cannot "see" amount because it is outside the scope of amount. The compiler will not compile this modified program.

QUESTION 5:

Can the toString() method see the object's instance variables, such as balance?