Hosting web - 254 Methods Chapter 6 The general format of
254 Methods Chapter 6 The general format of a method definition is return-value-type method-name( parameter-list ) { declarations and statements } The method-name is any valid identifier. The return-value-type is the data type of the result returned from the method to the caller. The return-value-type void indicates that a method does not return a value. Methods can return at most one value. The parameter-list is a comma-separated list in which the method declares each parameter s type and name. There must be one argument in the method call for each parameter in the method definition. Each argument also must be compatible with the type of the corresponding parameter in the method definition. For example, a parameter of type double can receive values of 7.35, 22 or 0.03546, but not “hello” (because a String cannot be assigned to a double variable). If a method does not receive any values, the parameter-list is empty (i.e., the name of the method is followed by an empty set of parentheses). Each parameter in the parameter list of a method must be declared with a data type; otherwise, a syntax error occurs. Following the first line of the method definition (also known as the method header), declarations and statements in braces form the method body. The method body is also referred to as a block. Variables can be declared in any block, and blocks can be nested. A method cannot be defined inside another method. There are three ways to return control to the statement that invoked a method. If the method does not return a result, control returns when the program flow reaches the method- ending right brace or when the statement return; is executed. If the method returns a result, the statement return expression; evaluates the expression, then returns the resulting value to the caller. When a return statement executes, control returns immediately to the statement that invoked the method. Note that the example in Fig. 6.3 actually contains two method definitions init (lines 13 41) and square(line 44 48). Remember that the applet container calls method init to initialize the applet. In this example, method init repeatedly invokes the square method to perform a calculation, then places the results in the JTextArea that is attached to the applet s content pane. When the applet appears on the screen, the results are displayed in the JTextArea. Notice the syntax used to invoke method square we use just the name of the method, followed by the arguments to the method in parentheses. Methods in a class definition are allowed to invoke other methods in the same class definition by using this syntax. (There is an exception to this rule, discussed in Chapter 8.) Methods in the same class definition are both the methods defined in that class and the inherited methods (the methods from the class that the current class extends JApplet in Fig. 6.3). We have now seen three ways to call a method: A method name by itself (as shown with square(x)in this example), a reference to an object followed by the dot (.) operator and the method name Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01