Optional Parameter
Some methods can take a parameter or supply a default parameter if one isn’t present. In such cases put the mandatory parameters first in the parameter list and add the optional parameters at the end. This makes as many of the parameters as possible the same, and the optional parameters appear as alternatives at the end.
The ServerSocket constructors demonstrate optional parameters. The basic constructor takes no arguments, but there is also a version with an optional port number and another version with an optional port number and an optional backlog length:
public ServerSocket()
public ServerSocket(int port)
public ServerSocket(int port, int backlog)
Languages with keyword parameters can express optional parameters more directly. Since Java has only positional parameters, whether a parameter is optional can only be expressed by convention. Some people call this the telescoping parameter pattern to provide a physical analogy for how the collection of parameters builds on each other.
|
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License |
