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


Account number. accountNumber    String  
Name of account holder. accountHolder   String  
Current balance. balance    int  

Implementing the Constructor

You might have thought of equally valid names. The account number should be a String because it is not expected to take part in arithmetic operations. Sometimes account numbers contain dashes or other non-digit characters. The balance is kept in terms of cents, so should be an int.

So far, the CheckingAccount class looks like this:

class CheckingAccount
  // instance variables
  String accountNumber;
  String accountHolder;
  int    balance;




Next, consider the constructor. The constructor has the same name as the class, and looks like this:

  CheckingAccount( parameter-list )
    initialize the data

The constructor is used with the new operator to create a new checking account object. It then initializes the account number, the account holder's name, and starting balance. Actual parameters are supplied to the constructor when it is used, such as in the following:

  CheckingAccount billsAccount = 
          new CheckingAccount( "123", "Bob", 100 ) ;

This statement creates a new CheckingAccount object by calling the constructor. The constructor will initialize the object's accountNumber to "123", its accountHolder to "Bob", and its balance to 100.


Fill in the parameter list for the constructor. You will have to think of names for the parameters, and will have to include the type of each parameter.

CheckingAccount(   ,   ,  )
  initialize the data