Java Reference
In-Depth Information
The withdraw method is very similar to the deposit method:
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
96
97
S YNTAX 3.5 The return Statement
return expression;
or
return;
Example:
return balance;
Purpose:
To specify the value that a method returns, and exit the method immediately. The
return value becomes the value of the method call expression.
There is only one method left, getBalance . Unlike the deposit and withdraw
methods, which modify the instance fields of the object on which they are invoked,
the getBalance method returns an output value:
public double getBalance()
{
return balance;
}
The return statement is a special statement that instructs the method to terminate
and return an output to the statement that called the method. In our case, we simply
return the value of the balance instance field. You will later see other methods that
compute and return more complex expressions.
Use the return statement to specify the value that a method returns to its caller.
Search WWH ::




Custom Search