public class BankAccount {
private long number; // account number
private long balance; // current balance (in cents)
public Permissions permissionsFor(Person who) {
Permissions perm = new Permissions();
perm.canDeposit = canDeposit(who);
perm.canWithdraw = canWithdraw(who);
perm.canClose = canClose(who);
return perm;
}
// ... define canDeposit et al ...
}
In methods that return a value, every path through the method must either return a value that is assignable to a
variable of the declared return type or throw an exception. The permissionsFor method could not return,
say, a String, because you cannot assign a String object to a variable of type Permissions. But you
could declare the return type of permissionsFor as Object without changing the return statement,
because you can assign a Permissions object reference to a variable of type Object, since (as you know)
all classes extend Object. The notion of being assignable is discussed in detail in Chapter 3.
2.6.5. Parameter Values
All parameters to methods are passed "by value." In other words, values of parameter variables in a method
are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter
is a copy of whatever value was being passed as an argument, and the method can change its parameter's
value without affecting values in the code that invoked the method. For example:
class PassByValue {
public static void main(String[] args) {
double one = 1.0;
System.out.println("before: one = " + one);
halveIt(one);
System.out.println("after: one = " + one);
}
public static void halveIt(double arg) {
arg /= 2.0; // divide arg by two
System.out.println("halved: arg = " + arg);
}
}
The following output illustrates that the value of arg inside halveIt is divided by two without affecting the
value of the variable one in main:
before: one = 1.0
halved: arg = 0.5
after: one = 1.0
You should note that when the parameter is an object reference, it is the object referencenot the object
itselfthat is passed "by value." Thus, you can change which object a parameter refers to inside the method