226 Methods Chapter 6 The programmer can (Web hosting faq)
Sunday, August 12th, 2007226 Methods Chapter 6 The programmer can write methods to define specific tasks that may be used at many points in a program. These methods sometimes are referred to as programmer-defined methods. The actual statements defining the method are written only once and are hidden from other methods. Methods are called by writing the name of the method (sometimes preceded by the class name and a dot operator), followed by a left parenthesis, the method s argument (or a comma-separated list of arguments) and a right parenthesis. All variables declared in method definitions are local variables they are known only in the method in which they are defined. Packaging code as a method allows that code to be executed from several locations in a program when the method is called. The return statement in a method passes the results of the method back to the calling method. The format of a method definition is return-value-type method-name( parameter-list ) { declarations and statements } The first line of a method definition is sometimes known as the method header. The attributes and modifiers in the method header are used to specify information about the method. The method return-value-type is the data type of the result that is returned from the method to the caller. Methods can return one value at most. The parameter-list is a comma-separated list containing the declarations of the parameters received by the called method. There must be one argument in the method call for each parameter in the method definition. The declarations and statements within the braces that follow the method header form the method body. Variables can be declared in any block, and blocks can be nested. A method cannot be defined inside another method. In many cases, an argument value that does not correspond precisely to the parameter types in the method definition is converted to the proper type before the method is called. When an argument is passed by value, a copy of the argument s value is made and passed to the called method. With pass-by-reference, the caller enables the called method to access the caller s data directly and to modify that data if the called method chooses. The class Random can be used to generate random numbers. An event is a signal that is sent to a program when some action takes place, such as when the user clicks a button. The programmer writes the application to perform tasks when these events occur. An event handler is a method that executes when an event occurs (or is raised ). An identifier s duration (its lifetime) is the period during which that identifier exists in memory. Identifiers that represent local variables in a method (i.e., parameters and variables declared in the method body) have automatic duration. Automatic-duration variables are created when program control reaches the variable s declaration. They exist while the block in which they are declared is active, and they are destroyed when the block in which they are declared is exited. The scope (sometimes called a declaration space) of an identifier for a variable, reference or method is the portion of the program in which that identifier can be referenced.
Check Tomcat Web Hosting services for best quality webspace to host your web application.