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

Do you want to keep hackers out of your checking account?



The private Visibility Modifier

When a member of a class is declared private it can be used only by the methods of that class. Here is the checking account class definition from the last chapter with each of its variables declared to be private.

class CheckingAccount
  // variable declarations
  private String accountNumber;
  private String accountHolder;
  private int    balance;

  CheckingAccount( String accNumber, String holder, int start )
    accountNumber = accNumber ;
    accountHolder = holder ;
    balance       = start ;

  // methods
  int getBalance()
    return balance ;

  void  processDeposit( int amount )
    balance = balance + amount ; 

  void processCheck( int amount )
    int charge;
    if ( balance < 100000 )
      charge = 15; 
      charge = 0;

    balance =  balance - amount - charge  ;


Now only the methods of a CheckingAccount object can "see" accountNumber, accountHolder, and balance.


It is useless to have a checking account if the balance cannot be changed. How can you change the balance in a CheckingAccount object?