by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 28: Defining and Invoking a method
Methods are the equivalent of procedures, functions, subroutines, subtasks as called in other languages.
In C language por example, a procedure is a task that does not return any value and a function
is a taks that returns a value.
In JAVA procedures and functions will be called methods. A method that does not return a value is
a void method.
Defining a method
In JAVA methods are defined within the class. It uses curly braces (as a class does) to encapsulate or group its definitions and contents.
Notice the set of parenthesis. Parameters can be passed within that set of parenthesis. More about that in a later Step.
A method 'displayHello' defined below: (in this example there are no parameters passed, and the method does not return any value as it's designed as void.
static void displayHello()
{
Invoking a method
A method can be invoked (called by) from any other method, by using just its name with the set of parenthesis. (Ex: read(); )
The parameters that are passed to the methods go inside that set of parenthesis. More about that in a later Step.
(See also below under class and Intance methods, next Step.)
by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 29: Instance and class(static) methods
In JAVA we have two types of Methods:
Class method
An example of a class method is the 'main' method.
As you know we use the 'static' keyword in the main
method definition.
A class method is such that it can be invoked directly by any other method of the class where is defined.
There is no need to instantiate an Object to access a class method.
It can be accessed from another class, by using the class name in front of the method followed by dot.
Let's say that we have a class method 'copy()' in the Exercise class, and it will be invoked from a method defined in another class;
then we write:
Example:
Instance method
An Instance method can be invoked directly from another Instance method of the same class.
If invoked from a class method, or from another method outside its class, then its class needs to be
instantiated (an Object created) and invoked using the Object reference.
Let's say that we have an Instance method 'displayReturn()' in the Exercise class,
and we would like to invoke it from another Instance method 'copy()'.
See the code below:
class Exercise
{
{
void displayReturn()
{
Let's take another Example. Let's say that we have the same Instance method 'displayReturn()' in the Exercise class,
and we would like to invoke it from another method 'insert()' of another class, 'Notebook'. In this case, we need to
instantiate the Exercise class, and we create an Object 'quiz'. This is done somewhere in the Notebook class.
In our example below, that's done within the 'insert()' method.
See below for the code in the Notebook class:
class Notebook
{
{
quiz.displayReturn(); // method invoked. Notice the Object reference.
You notice that displayReturn() method is defined within the Exercise class. (See previous Example.)
Let's take yet another Example. Let's say that we have the same Instance method 'displayReturn()' in the Exercise class,
and we would like to invoke it from a class method, say the main method. In this case, we also need to
instantiate the Exercise class to create an Object 'quiz'.
In the example below, we do this within the 'main' method.
See below for the code in the Exercise class:
class Exercise
{
{
quiz.displayReturn(); // method invoked. Notice the Object reference.
void displayReturn()
{
by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 30: Returning a value by a method
Method may or may not return a value.
Return type: The return type is used when defining a method. It's the word preceding the method name.
Example of a definition of a method that returns an 'int' type.:
{
return number; // value of data type 'int' is returnded.
Example:
{
return; // No need to use the 'return' keyword.
// If used, it should immediately be followed by a comma, as there's nothing to be returned.
Example:
{
return test; // the Returned value 'test' is of the class Exercise type.
When invoking a method that is not void (returns a value), it is convenient to assign it to a value.
Example: (in this example the method getName() returns a String type.)
{
{
by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 31: Parameters Passed via a Method
Parameters are passed when the method is called.
They are placed within the set of parentheses that follow the method's name.
The parameters' types have to be stated in the method definition.
If no parameters are passed, there will be an empty set of parentheses.
Examples of methods calls:
setMsg("Message"); // a String parameters is passed.
add(8, 9); // two 'ints' are passed.
subtract(a, b); // parameters a and b are passed.
{
When the method is invoked from the calling unit, parameters passed can be either variables or literals.
Example of a method call passing three different types of parameters:
by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 32: Parameters Passed as Values, and as References
Parameters can be values or references.
One of the important characteristics that distinguish JAVA from C and C++ is the fact in JAVA parameters of the
primitive types (int, char, double, etc.) are passed as values, and there are no pointers made to reference them.
By the same token, in JAVA, any non-primitive parameters (Objects) are passed as references.
It is important to notice than when passing a parameter to a method, a local variable of the method is used to capture that
parameter. The local variable in the method is not the same as the variable used in the calling unit.
When that parameter is passed as a value, any modification made to the variable that conveys that parameter within the method,
DOES NOT AFFECT the counterpart variable in the calling unit.
If the parameter is passed as a reference, any modification made to the variable that conveys that parameter within the method,
DOES AFFECT the counterpart variable of the calling unit.
It is also convenient to state again, that in JAVA all primitive data types parameters are passed as values, and
all non-primitive data types are passed as references, and the programmer does not have any choice upon this.
To better understand the difference between passing a parameter as value and as reference, let's see the two Examples below.
Example: (passed as value)
{
{
passNumber(number); // 'number' passed as value - control flow goes to method below.
System.out.println(number); // still 45 after returning from method.
static void passNumber(int number)
{
System.out.println(number); // displays 46;
// when method is done, control flows back to the 'main' the calling unit.
Notice that in the Example above (passed by value) the primitive data type variable that was modified in the method,
was not affected in the calling unit, the main method.
Example: (passed as reference)
{
{
msg = new StringBuffer("Hello"); // create the Object 'msg' of StringBuffer class.
passMessage(msg); // 'msg' passed as reference - control flow goes to method below.
System.out.println(msg); // 'msg' is now "Hello World" as modified in method.
static void passMessage(StringBuffer newMsg)
{
System.out.println(newMsg); // displays "Hello World"
// when method is done, control flows back to the 'main' the calling unit.
Notice that in the Example above (passed by reference) the object of StringBuffer type was modified in the method, and
it was also modified in the calling unit, the main method.
by
PRO
FE S
S OR

SECTION VIII - Methods and Overloading
Step 33: Method Overloading
Method Overloading
Method Overloading refers to various methods having the same name within the same class.
Answer: Because their signatures are not the same.
Question: What is the signature of a method?
Answer: A signature is represented by the parameters types in a particular order as defined in the method.
Two methods can have the same name (in the same class) and as long as the signatures are different, the compiler can distinguish
among the methods to implement the correct one during their invocation. The signature is what makes the method unique.
The return type of the method is not part of the signature and would not affect overloading, that is, methods still can be
overloaded with different return types, as long as their signatures are different.
In the next example, we have 4 methods each one called 'process' and each one with a different signature.
{
{
process (5.4, 12); // one 'double' and one 'int' (in that order) comprises the signature.
double result = process (6.4, 12.5); // two 'doubles' comprises the signature.
process (7, 11.3); // one '
// Method 1: Signature comprises two 'int's
static void process(int a, int b)
{
// Method 2: Signature comprises one 'double' and one 'int' in that order.
static void process(double a, int b)
{
// Method 3: Signature comprises two 'double's, and the return type if a 'double'.
static double process(int a, int b)
{
// processing code goes here
return result;
// Method 4: Signature comprises one 'int' and one 'double' in thar order.
static void process(int a, int b)
{
In overloading methods, we can see the convenience of having methods with the same name. In the PrintStream class of JAVA, the
method 'println()' is overloaded to accept all the primitive data types, and Strings. There are many println() methods with
the different signatures for the various data types.
In the case of Constructors (discussed in later Section), overloading becomes more relevant, as many Constructors can exist
within the same class, and therefore, different initializations for Objects can be easily accomplished.