Page 250 - Introduction to Programming with Java: A Problem Solving Approach
P. 250
216 Chapter 6 Object-Oriented Programming
grow method is improved. The getAge method retrieves a mouse’s age. Remember the age variable is private, so the only way for the outside world to read a mouse object’s age is to use a public method— the getAge method. The getWeight method retrieves a mouse’s weight. The grow method simulates a mouse’s growth for a specified number of days. Note the days parameter. The number of days is passed into the days parameter and that’s how the method knows how many days to simulate.
Here are some of the standard UML class diagram features not found in Figure 6.3 that do appear in Figure 6.12:
• To specify member accessibility, prefix all member specifications with a “-” for private access or a “+” for public access. The instance variables have “-” prefixes, since we want them to be private, and the methods have “+” prefixes, since we want them to be public.
• To specify initialization, append “= <value>” to each variable declaration that includes initialization. For example, note the “ = 0” after the age instance variable’s specification.
• Underline the main method in the MouseDriver class diagram box, since the main method is de- clared with the static modifier. UML standards suggest that you underline all methods and variables that are declared with the static modifier. As you learned in Chapter 5, the static modifier indi- cates a class member. You’ll learn more about class members in Chapter 9.
• Include a “: <type>” suffix with each method. This specifies the type of value that the method returns. All the methods in the Mouse class in Figure 6.4 returned void (nothing), but in Chapter 5 you saw many Java API class methods with return types like int and double, and we’ll discuss implementa- tion of such methods later in this chapter.
Figure 6.12 also includes an extra UML class diagram feature. It has notes for two of its methods—the main and grow methods. The nAotpesaregdoepictePdDbyFthe rEectnanhgleas wnitch teherbent top-right corners. Why bent corners? They are supposed to give the impression of a piece of paper with its corner folded, an indica- tion of a hardcopy “note.” Including a note in a UML class diagram is purely optional. Usually we won’t use them, but this time, we did use them because we wanted to show how you can include local variables in a UML class diagram.
6.9 Local Variables
A local variable is a variable that’s declared and used “locally” inside a method. That’s different from an instance variable, which is declared at the top of a class, outside all methods. As you perhaps now realize, all the variables we defined in chapters prior to this chapter were local variables. They were all declared within main methods, so they were all local variables within the main method. We didn’t bother to explain the term “local variable” until now because there were no other methods besides main, and the idea of a variable being local to main wouldn’t have made much sense. But the OOP context makes the concept of a local variable more meaningful.
Scope
A local variable has local scope—it can be used only from the point at which the variable is declared to the end of the variable’s block. A variable’s block is established by the closest pair of braces that enclose the variable’s declaration. Most of the time, you should declare a method’s local variables at the top of the method’s body. The scope of such variables is then the entire body of the method.
for loop index variables are local variables, but they are special. Their scope rule is slightly different from what is described above. As you know from Chapter 4, you should normally declare a for loop’s index