Home Segments Top Top Previous Next

133: Mainline

The following program defines movieRating, albeit awkwardly, to illustrate the limited availability of parameter values:

public class Demonstrate { 
 // First, define adder: 
 public static int adder () { 
  return s + a + d;                                     // BUG! 
 } 
 // Next, define movieRating: 
 public static int movieRating (int s, int a, int d) { 
  return adder();                                       // BUG! 
 } 
 // Then, define main: 
 public static void main (String argv[]) { 
  int script = 6, acting = 9, direction = 8, result; 
  result = movieRating(script, acting, direction); 
  System.out.print("The rating of the movie is "); 
  System.out.println(s + a + d);                        // BUG! 
 } 
} 

In this program, movieRating asks adder—a method with no parameters—to perform the computation of s + a + d. However, the Java compiler cannot compile adder, because values for the s, a, and d parameters of the movieRating method are not available in expressions that lie outside of the definition of movieRating, and thus, they are not available in the adder method.

Moreover, Java cannot compile the second print statement in the main method, because values for the s, a, and d parameters of the movieRating method are not available in expressions that lie outside of the definition of movieRating, and thus, they are not available in the main method.