| Methods in Java can accept information from others methods and can process
that information to do useful tasks. The data that is passed into a method is
known as the parameter list. A method may take one parameter, two or more
parameters or no parameters at all. |
|
| Examples of functions using a different number of parameters: |
|
|
number of
parameters
|
Sample Method call |
| 0 |
temp = keyboard.readLine( );
|
| 1 |
System.out.println("hello");
|
| 2 |
File1.read(stringbuf, 64);
|
|
|
| Let us examine a user defined function. Look at the Java Program listed
below: |
|
public class Methods_Sample {
public static int doubleIt( int x) {
int y = x+x;
// y is known as a local variable
return y;
} // end "doubleIt"
method
public static void main (String[ ] args){
int someNumber = 5;
int doubleNumber;
doubleNumber = doubleIt( someNumber );
System.out.println("The Number is
" + someNumber );
System.out.println("the Number
doubled= " + doubleNumber);
} // end main method
} // end of class
|
|
| If you look at the method declaration shown below, you will see that this
user defined function takes only one parameter (in the method header) which
happens to be an integer and has the parameter name "x". |
|
| public
static int doubleIt( int x) |
Method
Header |
{
int y = x+x;
return y;
} // end "doubleIt"
method |
Method
Body |
|
|
| In the main program, when the function "doubleIt( )"
is invoked, the number (or variable) that is placed between the brackets of
the function header (ie: doubleIt(5) or doubleIt(var)) gets sent to the function and is stored in the local variable
"x" shown in the parameter list. The local variable "x"
only exists in the method doubleIt( ) and can not be
accessed outside of that method. |
| Invoking (executing) the user-defined method doubleIt(
) from
the main program looks like this ... |
|
|
doubleNumber = doubleIt (someNumber ); |
|
| This is known as a method call. At this point, the execution jumps from the main( )
method
and starts executing in the doubleIt( ) method. |
|
|
public
static int doubleIt ( int x )
{
int y = x+x;
return y;
} |
|
| When the method doubleIt() is completed, it will jump
back into the main( ) method where it jump out and
continue on. |
|
|
doubleNumber
= doubleIt(someNumber );
System.out.println ("The Number is " + someNumber );
System.out.println ("Double the Number is " + doubleNumber); |
|
| Note that because this method returned a value, that the variable "doubleNumber"
will be changed (when the doubleIt() method is
done) so that it will store the return value of the doubleIt() method. |
|
| Normally, arguments are passed into a function by a process known as
passing by value, which means that a copy of what is stored in
the variable that is used in the parameter list is given to the function.
When you change the value of the parameter in the function, you are only
changing the copy in the current method, but the original value stays the same
(back in the calling method). By default, all primitive variable types are
passed by value (ie: int, double, float, char ... ). |
|
| However, parameters may be changed back in the calling method if those
parameter are passed by reference.
When this is done, changing the argument's value in the method also
changes the original value as well. When you call a method with object as
a parameter (not a primitive, but an object), the variables you pass into the
body of the method are passed by reference (whatever you do to those objects
inside the method affects the original objects as well). This includes arrays
and all of the objects that the arrays contain.. |