Java Reference
In-Depth Information
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 re-
turn 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 Ob-
ject , 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;
 
Search WWH ::




Custom Search