412 Object-Based Programming Chapter 8 available and how (Web hosting resellers)

412 Object-Based Programming Chapter 8 available and how closely they meet software developer requirements, and the like. Many interesting research and development problems have been solved and many more need to be solved; these problems will be solved because the potential value of software reuse is enormous. 8.10 Final Instance Variables We have emphasized repeatedly the principle of least privilege as one of the most fundamental principles of good software engineering. Let us see one way in which this principle applies to instance variables. Some instance variables need to be modifiable and some do not. The programmer can use the keyword finalto specify that a variable is not modifiable and that any attempt to modify the variable is an error. For example, private final int INCREMENT = 5; declares a constant instance variable INCREMENTof type intand initializes it to 5. Software Engineering Observation 8.17 Declaring an instance variable as final helps enforce the principle of least privilege. If an instance variable should not be modified, declare it to be final to expressly forbid modification. Testing and Debugging Tip 8.3 Accidental attempts to modify a final instance variable are caught at compile time rather than causing execution-time errors. It is always preferable to get bugs out at compile time, if possible, rather than allowing them to slip through to execution time (where studies have found that the cost of repair is often as much as ten times more expensive). Common Programming Error 8.9 Attempting to modify a final instance variable after it is initialized is a syntax error. The applet of Fig. 8.11 creates a final instance variable INCREMENT of type int and initializes it to 5 in its declaration (line 15). A finalvariable cannot be modified by assignment after it is initialized. Such a variable can be initialized in its declaration or in every constructor of the class. Common Programming Error 8.10 Not initializing a final instance variable in its declaration or in every constructor of the class is a syntax error. 1 // Fig. 8.11: Increment.java 2 // Initializing a final variable 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 Fig. 8.11Initializing a finalvariable (part 1 of 2). Fig. 8.11 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply