Access Methods

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:

Use processDeposit() or processCheck().

Access Methods

A class with private data provides access to that data through access methods. An access method is a method which uses the private data of its object and is visible to other classes. Some access methods alter data; others return a value but don't alter data. Here is a main() that uses the access methods of a CheckingAccount object.

class CheckingAccount
{
  private String accountNumber;
  private String accountHolder;
  private int    balance;
  . . . .
}

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

    System.out.println( bobsAccount.getBalance() );
    bobsAccount.processDeposit( 200 );
    System.out.println( bobsAccount.getBalance() );

  }
}

This main() program uses access methods of the CheckingAccount object to change the object's data. This is (usually) the correct way to use an object. The methods of an object know best how to change its instance variables. The idea of private is to enforce this correct use.

QUESTION 3:

Could an access method check for errors and change instance variables only if the data is correct?