296 Methods Chapter 6 For now, we do (Web server hosting)

296 Methods Chapter 6 For now, we do not concern ourselves with operation parameters or return types; we attempt to gain only a basic understanding of the operations of each class. As we continue our design process, the number of operations belonging to each class may vary we might find that new operations are needed or that some current operations are unnecessary and we might determine that some of our class operations need non-void return types. SUMMARY The best way to develop and maintain a large program is to divide it into several smaller modules. Modules are written in Java as classes and methods. A method is invoked by a method call. The method call mentions the method by name and provides arguments in parentheses that the called method requires to perform its task. If the method is in another class, the call must be preceded by a reference name and a dot operator. If the method is static, it must be preceded by a class name and a dot operator. Each argument of a method may be a constant, a variable or an expression. A local variable is known only in a method definition. Methods are not allowed to know the implementation details of any other method (including its local variables). The on-screen display area for a JApplethas a content pane to which the GUI components must be attached so they can be displayed at execution time. The content pane is an object of class Containerfrom the java.awt package. Method getContentPane of class JAppletreturns a reference to the applet s content pane. The general format for a method definition is return-value-type method-name( parameter-list ) { declarations and statements } The return-value-type states the type of the value returned to the calling method. If a method does not return a value, the return-value-type is void. The method-name is any valid identifier. The parameter-list is a comma-separated list containing the declarations of the variables that will be passed to the method. If a method does not receive any values, parameter-list is empty. The method body is the set of declarations and statements that constitute the method. The arguments passed to a method should match in number, type and order with the parameters in the method definition. When a program encounters a method, control transfers from the point of invocation to the called method, the method executes and control returns to the caller. A called method can return control to the caller in one of three ways. If the method does not return a value, control returns at the method-ending right brace or by executing the statement return; If the method does return a value, the statement return expression; returns the value of expression. There are three ways to call a method the method name by itself, a reference to an object followed by the dot (.) operator and the method name, and a class name followed by the dot (.) operator and a method name. The last syntax is for staticmethods of a class. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply