Java Reference
In-Depth Information
Exercise 2.12 : Considering your Vehicle and LinkedList classes, can
you think of a need for any methods that take a variable number of ar-
guments?
2.6.4. Method Execution and Return
When a method is invoked, the flow of control passes from the calling
method into the invoked method and the statements of that method are
executed in sequence according to the semantics of those statements. A
method completes execution and returns to the caller when one of three
things happens: a return statement is executed, the end of the method
is reached, or an uncaught exception is thrown.
If a method returns any result, it can only return a single resulteither
a primitive value or a reference to an object. Methods that need to re-
turn more than one result can achieve this effect in several ways: return
references to objects that store the results as fields, take one or more
parameters that reference objects in which to store the results, or re-
turn an array that contains the results. Suppose, for instance, that you
want to write a method to return what a particular person can do with
a given bank account. Multiple actions are possible (deposit, withdraw,
and so on), so you must return multiple permissions. You could create
a Permissions class whose objects store boolean values to say whether a
particular action is allowed:
public class Permissions {
public boolean canDeposit,
canWithdraw,
canClose;
}
Here is a method that fills in the fields to return multiple values:
public class BankAccount {
private long number; // account number
private long balance; // current balance (in cents)
 
Search WWH ::




Custom Search