Java Reference
In-Depth Information
Notice also that the method
getErrorMsg
is defined twice in
ErrorMsg
. The first
method specification accepts no parameters, and the second accepts one parameter
of type
int
. This parameter's name (as used internally by the method) is
msgCode
.
M
ETHOD
O
VERLOADING
Having two variations on the same method is an example of
method overloading
.
This is another mechanism to request a specific function in the class
ErrorMsg
.
Methods with the same name can be defined to require variable numbers or differ-
ent types of parameters. Consumer (calling) classes can request any of these public
methods. The compiler will examine a method call, including its input parameters
and return type, and call the correct public method of the class.
C
ALLER
C
LASS
After the
ErrorMsg
class has been properly compiled, a consumer class can use these
various methods in the following way:
// Define an instance of the ErrorMsg class.
ErrorMsg myErrorMsg = new ErrorMsg ();
// Define a few local variables.
int msgCode;
String myString;
...
// Get the error message from myErrorMsg; store the result in myString.
// Get the error message from myErrorMsg based on the integer value in
// msgCode.