| To use a method you must invoke that method. Invoking a method is done
by referring to method's name and specifying which parameters are to be sent to
the method. Additionally, if the function has a return value, you can use that
return value in your program. |
| Let's look at a sample Java program and how to invoke a method. |
|
Sample#1: The Hello World program
|
|
class FirstApp {
public static void main(String[ ] args) {
System.out.println("Hello World");
}
} |
|
| The method "System.out.println( )"
was invoked (it was executed) by simply specifying the name of the method,
specify the parameter to be sent to the method (in this case the
parameter is "Hello World" ) and ending the Java
statement with a semi-colon ( ;). This is known as a method call. |
|
Sample#2: Calling a method that is in a library
|
| Let's examine the example we used for keyboard input |
|
import java.io.*;
public class Class1
{
public static void main (String[ ] args)
{
DataInput keyboard = new DataInputStream(System.in);
String InputText;
try {
InputText = keyboard.readLine(
);
System.out.println("you
typed: " + InputText);
} // end try
catch (IOException e) {
System.out.println("There
was an error");
} // catch
} // end main
} // end class
|
|
| When we learned about inputting information from the keyboard, we learned
that in order to do interactive input in Java we had to import the java.io library.
This was done using the code import
java.io.*;. Importing the library allowed us to use
the methods that were created in that library. in this case we used the method
"readLine( )" from the java.io library.
If you forgot to import the java.io library, you noticed that your
program would not compile because the method "readLine( )"
was undeclared . |
| Involving the "readLine( )" is as simple as specifying
the name of the method, and ending the Java statement with a semi-colon
( ;) |
|
|
InputText = keyboard.readLine(
); |
|
Sample#3: Calling a user defined method
|
| Let us examine a user defined function. Look at the Java Program listed
below: |
|
public class Methods_Sample
{
public static double changeFtoC( double fTemp) {
double cTemp = ( fTemp - 32 ) / 1.8 ;
return cTemp;
} // end changeFtoC
public static void main (String[ ] args) {
double aFahrenheitTemp = 5;
double theCelsiusEquivalent;
theCelsiusEquivalent =
changeFtoC ( aFahrenheitTemp );
System.out.println ("The
Fahrenheit temperature is " + aFahrenheitTemp );
System.out.println ("The Celsius
temperature is " + theCelsiusEquivalent );
} // end main
} // end class
|
|
| Invoking (executing, calling) the user-defined method changeFtoC( ) from
the main program is done like this: |
|
|
theCelsiusEquivalent = changeFtoC( aFahrenheitTemp ); |
|
| When the function "changeFtoC( )" is invoked in the
main method, the variable (or number) that is found between the brackets of
the function header (ie: changeFtoC()) gets sent to the function and is stored in the local variable
"fTemp" shown in the parameter list of changeFtoC(). |
| At this point, the execution of the Java program jumps from the main(
)method and starts executing in the changeFtoC() method. |
| You should note that "fTemp" now stores the value that was found in
the variable "aFahrenheitTemp" back in the main method. |
|
|
double cTemp = ( fTemp - 32 ) / 1.8 ;
return cTemp;
|
|
| The variable "cTemp" is an example of
a local variable. This variable is only available to (can only be
referenced from) program lines inside the method. This is known as its scope.
The "cTemp" variable only exists while the method is working.
Local variables are created when the method is called and destroyed
when the block of code that they are a part of is exited. When the method changeFtoC() is completed, it will jump
back into the main( ) method to the spot where it jump out and
continue on. |
|
|
theCelsiusEquivalent = changeFtoC (
aFahrenheitTemp );
System.out.println ("The Fahrenheit temperature is " + a
FahrenheitTemp );
System.out.println ("The Celsius temperature is " +
theCelsiusEquivalent );
|
|
| Because this method returned a value, the variable "theCelsiusEquivalent"
will be changed so that it will store the return value of the changeFtoC()
method (in this case what ever was found in "cTemp" before
the changeFtoC() method was completed). |
|