Name | Type | |
---|---|---|
Account number. | accountNumber | String |
Name of account holder. | accountHolder | String |
Current balance. | balance | int |
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 }
Remember that we are defining a class, so it does not make sense to assign initial values to the variables in the data declarations section (since each individual object will have different values.) Next, consider the constructor. The constructor will have the same name as the class, so it will look something like this:
CheckingAccount( parameter-list ) { initialize the data }
Recall from the requirements what we want the constructor to do: it should create a new checking account and initialize it with the account number, the account holder's name, and starting balance.
The constructor does not need any statements that actually create
an object out of main storage (this happens automatically.)
It does need to initialize the object's data after it has been
created.
When a CheckingAccount
constructor is used the call
will provide actual parameters, 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.