Java Reference
In-Depth Information
So far we have discussed three locations where a method can store and access
data:
member variables - the data defined in the data fields of the class definition
local variables - data declared within a method and only valid there
method parameters - data passed in the method parameter list
Forexample, the above triple() method includes all three types of data: a
member variable, local variables, and the parameter variables. Note that you can
also access data in another object if the access rule for that data allows it.
3.3.3 Method overloading
Perhaps you create a class that holds a method with an integer parameter:
void aMethod (int k) { ... }
Yo u decide later that you need a method that accomplishes essentially the same
task but requires a float parameter. In some procedural languages you would
create a new method with a slightly different name:
void aMethod - f (float x) {...}
A more elegant solution would allow you to use the exact same name for the new
method and have the compiler determine from the parameter type which method
to use:
void aMethod (int k) {...}
void aMethod (float x) {...}
This very valuable feature is called overloading ,which Java permits for methods
as well as constructors. Another example of overloading is when the number
of parameters changes. For example, there can be yet another method named
aMethod() that takes two int parameters instead of just one:
void aMethod (int k, int q) { ... }
Constructors with different numbers of parameters are common in the stan-
dard Java class libraries in which none, one, two, or more class properties can
be initialized using the same constructor name but with different parameter
lists.
Another common example is the println() method that we've already used
to print messages to the Java console:
System.out.println (String)
Search WWH ::




Custom Search