|
|
| BankAccount.java |
/**
* Create a new bank account with the given balance
*/
public BankAccount(int initialAmount) {
balance = initialAmount;
}
|
![]() | More than one constructor may be declared for a class,
provided that they can all be distinguished by their
signature, i.e. the number of parameters and their types.
|
![]() | The selection of a particular constructor depends then
on the parameter list given to the new operator.
|
![]() | Example 1: account = new BankAccount();
The default constructor is taken as no parameters are
provided. Consequently, the balance is initialized to 0.
|
![]() | Example 2: account = new BankAccount(1000);
The other constructor (above) is taken and the balance
is set to 1000 cents.
|
![]() | If just the second constructor above would have been
provided, no default constructor would be available and
a new operator without parameters would not permitted,
i.e. example 1 would be rejected by the compiler.
|
|
| Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002 |