Implementing the Constructor

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:

 NameType
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;

   constructors

   methods

}

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.

QUESTION 6:

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
}