The name and orbits fields are hidden from the constructor by the parameters of the same name. To
access, for example, the name field instead of the name parameter, we prefix it with this to specify that the
name is for the field belonging to "this" object. Deliberately hiding identifiers in this manner is considered
acceptable programming practice only in this idiomatic use in constructors and "set" methodssome coding
guidelines and development environments advocate never hiding identifiers, even in these cases. The way in
which names are resolved is discussed in "The Meanings of Names" on page 178.
2.8. Overloading Methods
Each method has a signature, which is its name and the number and types of its parameters. Two methods can
have the same name if they have different numbers or types of parameters and thus different signatures. This
feature is called overloading because the simple name of the method has an overloaded (more than one)
meaning. When you invoke a method, the compiler uses the number and types of arguments to find the best
match from the available overloads. Here are some orbitsAround methods for our Body class that return
TRue if the current body orbits around the specified body or a body with the specified identifier:
public boolean orbitsAround(Body other) {
return (orbits == other);
}
public boolean orbitsAround(long id) {
return (orbits != null && orbits.idNum == id);
}
Both methods declare one parameter, but the type of the parameter differs. If the method orbitsAround is
invoked with a Body reference as an argument, the version of the method that declares a Body parameter is
invokedthat version compares the passed in reference to the body's own orbits reference. If
orbitsAround is invoked with a long argument, the version of the method that declares a long
parameter is invokedthat version of the method compares the passed in identification number with the idNum
field of the object it orbits. If the invocation matches neither of these signatures, the code will not compile.
For varargs methods, a sequence parameter T... is treated as being a parameter of type T[] for overloading
purposes. So if two signatures differ only because one declares a sequence and the other an array, then a
compile-time error occurs.
The signature does not include the return type or the list of thrown exceptions, and you cannot overload
methods based on these factors. A full discussion of how the language chooses which available overloaded
method to invoke for a given invocation can be found in "Finding the Right Method" on page 224.
As you may have realized, constructors can also be overloaded in the same way that methods are.
Overloading is typically used when a method or constructor can accept the same information presented in a
different form, as in the orbitsAround example, or when it can use a number of parameters, some of
which have default values and need not be supplied. For example, the Body class might have two overloaded
constructors: one that takes a name and another Body object that it orbits around (as shown on page 55) and a
second that takes only a name, indicating that body does not orbit another body.
You should use overloading judiciously and carefully. While overloading based on the number of arguments
is usually quite clear, overloading based on the same number but different types of arguments can be
confusing. When you add varargs methods to the mix, even overloading based on different numbers of
parameters can become confusing when reading or writing invocations of those methodsdoes that
two-argument invocation match two fixed parameters, or one fixed parameter and a sequence with one