Java Reference
In-Depth Information
{
public BankAccount()
{
// Generates next account number to be assigned
lastAssignedNumber++;
// Updates the static field
// Assigns field to account number of this bank account
accountNumber = lastAssignedNumber;
// Sets the
instance field
}
. . .
}
354
355
How do you initialize a static field? You can't set it in the class constructor:
public BankAccount()
{
lastAssignedNumber = 1000;
// NOÈŒwould reset to 1000 for
each new object
. . .
}
Then the initialization would occur each time a new instance is constructed.
Figure 4
A Static Field and Instance Fields
355