Web hosting e commerce - 298 Methods Chapter 6 Each variable identifier
Monday, December 31st, 2007298 Methods Chapter 6 Each variable identifier has the attributes duration (lifetime) and scope. An identifier s duration determines when that identifier exists in memory. An identifier s scope is where the identifier can be referenced in a program. Identifiers that represent local variables in a method have automatic duration. Automatic-duration variables are created when program control reaches their 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. Java also has identifiers of static duration. Variables and references of static duration exist from the point at which the class in which they are defined is loaded into memory for execution until the program terminates. The scopes for an identifier are class scope and block scope. An instance variable declared outside any method has class scope. Such an identifier is known in all methods of the class. Identifiers declared inside a block have block scope. Block scope ends at the terminating right brace (}) of the block. Local variables declared at the beginning of a method have block scope, as do method parameters, which are considered to be local variables of the method. Any block may contain variable declarations. A recursive method is a method that calls itself, either directly or indirectly. If a recursive method is called with a base case, the method returns a result. If the method is called with a more complex problem, the method divides the problem into two or more conceptual pieces: A piece that the method knows how to do and a slightly smaller version of the original problem. Because this new problem looks like the original problem, the method launches a recursive call to work on the smaller problem. For recursion to terminate, the sequence of smaller and smaller problems must converge to the base case. When the method recognizes the base case, the result is returned to the previous method call, and a sequence of returns ensues all the way up the line until the original call of the method returns the final result. Both iteration and recursion are based on a control structure: Iteration uses a repetition structure; recursion uses a selection structure. Both iteration and recursion involve repetition: Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated method calls. Iteration and recursion each involve a termination test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized. Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges to the base case. Recursion repeatedly invokes the mechanism and, consequently the overhead, of method calls. This repetition can be expensive in terms of both processor time and memory space. The user presses the Enter key while typing in a JTextFieldto generate an action event. The event handling for this GUI component is set up like a JButton: Aclass must be defined that implements ActionListener and defines method actionPerformed. Also, the JText- Field s addActionListener method must be called to register the event. It is possible to define methods with the same name, but different parameter lists. This feature is called method overloading. When an overloaded method is called, the compiler selects the proper method by examining the arguments in the call. Overloaded methods can have different return values and must have different parameter lists. Two methods differing only by return type will result in a syntax error. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01