Java Reference
In-Depth Information
BankAccount harrysChecking = new BankAccount(1000);
one step at a time. Here are the steps that are carried out when the statement executes.
ȗ Create a new object of type BankAccount .
ȗ Call the second constructor (since a construction parameter is supplied).
ȗ Set the parameter variable initialBalance to 1000.
ȗ Set the balance instance field of the newly created object to
initialBalance .
ȗ Return an object reference, that is, the memory location of the object, as the
value of the new expression.
ȗ Store that object reference in the harrysChecking variable.
Let's move on to implementing the BankAccount methods. Here is the deposit
method:
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
To understand exactly what the method does, consider this statement:
harrysChecking.deposit(500);
This statement carries out the following steps:
ȗ Set the parameter variable amount to 500.
ȗ Fetch the balance field of the object whose location is stored in
harrysChecking .
ȗ Add the value of amount to balance and store the result in the variable
newBalance .
ȗ Store the value of newBalance in the balance instance field, overwriting
the old value.
Search WWH ::




Custom Search