34.13. Programming Exercises

 
[Page 131 ( continued )]

5.3. Calling a Method

In creating a method, you give a definition of what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not.

If the method returns a value, a call to the method is usually treated as a value. For example,

   int   larger = max(   3   ,   4   ); 

calls max(3, 4) and assigns the result of the method to the variable larger . Another example of a call that is treated as a value is

 System.out.println(max(   3   ,   4   )); 

which prints the return value of the method call max(3, 4) .

If the method returns void , a call to the method must be a statement. For example, the method println returns void . The following call is a statement:

 System.out.println(   "Welcome to Java!"   ); 

Note

A method with a nonvoid return value type can also be invoked as a statement in Java. In this case, the caller simply ignores the return value. This is rare, but permissible if the caller is not interested in the return value.


When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.

Listing 5.1 shows a complete program that is used to test the max method. The output of the program is shown in Figure 5.2.

Figure 5.2. The program invokes max(i, j) in order to get the maximum value between i and j .



[Page 132]
Listing 5.1. TestMax.java
 1   public class   TestMax {  2  /** Main method */  3   public static void   main(String[] args) {  4   int   i = 5;  5   int   j = 2;  6   int   k =  max(i, j)  ;  7      System.out.println(   "The maximum between "   + i +  8   " and "   + j +   " is "   + k);  9    } 10 11  /** Return the max between two numbers */  12    public static int   max(   int   num1,   int   num2)  { 13   int   result; 14 15   if   (num1 > num2) 16        result = num1; 17   else   18        result = num2; 19 20    return   result;  21    } 22  } 

This program contains the main method and the max method. The main method is just like any other method except that it is invoked by the JVM.

The main method's header is always the same, like the one in this example, with the modifiers public and static , return value type void , method name main , and a parameter of the String[] type. String[] indicates that the parameter is an array of String , a subject addressed in Chapter 6, "Arrays."

The statements in main may invoke other methods that are defined in the class that contains the main method or in other classes. In this example, the main method invokes max(i, j) , which is defined in the same class with the main method.

When the max method is invoked (line 6), variable i 's value 5 is passed to num1 , and variable j 's value 2 is passed to num2 in the max method. The flow of control transfers to the max method. The max method is executed. When the return statement in the max method is executed, the max method returns the control to its caller (in this case the caller is the main method). This process is illustrated in Figure 5.3.

Figure 5.3. When the max method is invoked, the flow of control transfers to the max method. Once the max method is finished, it returns the control back to the caller.


[Page 133]

Caution

A return statement is required for a nonvoid method. The method shown below in (a) is logically correct, but it has a compilation error because the Java compiler thinks it possible that this method does not return any value.

To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return statement to be reached regardless of how the if statement is evaluated.


Note

One of the benefits of methods is for reuse. The max method can be invoked from any class besides TestMax . If you create a new class, Test , you can invoke the max method using ClassName.methodName (i.e., TestMax.max ).


5.3.1. Call Stacks

Each time a method is invoked, the system stores parameters and variables in an area of memory, known as a stack , which stores elements in last-in first-out fashion. When a method calls another method, the caller's stack space is kept intact, and new space is created to handle the new method call. When a method finishes its work and returns to its caller, its associated space is released.

Understanding call stacks helps you to comprehend how methods are invoked. The variables defined in the main method are i , j , and k . The variables defined in the max method are num1 , num2 , and result . The variables num1 and num2 are defined in the method signature and are parameters of the method. Their values are passed through method invocation. Figure 5.4 illustrates the variables in the stack.

Figure 5.4. When the max method is invoked, the flow of control transfers to the max method. Once the max method is finished, it returns the control back to the caller.

Tip

If you use an IDE such as JBuilder, NetBeans, or Eclipse, please refer to Learning Java Effectively with JBuilder/NetBeans/Eclipse in the supplements. This supplement shows you how to use a debugger to trace method invocations.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net