Page 236 - Introduction to Programming with Java: A Problem Solving Approach
P. 236
202 Chapter 6 Object-Oriented Programming
tion? That’s called an initialization. Note the initializations for age and weight. We initialize age to 0 because newborn mice are zero days old. We initialize weight to 1 because newborn mice weigh approxi- mately 1 gram.
The primary difference between instance variable declarations and variable declarations you’ve seen in the past is the private access modifier. If you declare a member to be private, then the member can be accessed only from within the member’s class and not from the “outside world” (i.e., by code that’s outside of the class in which the member resides). Instance variables are almost always declared with the private access modifier because you almost always want an object’s data to be hidden. Making an in- stance variable private gives you control over how its value can be changed. For example, you could as- sure that a weight is never made negative. Constraining data access is what encapsulation is all about, and it’s one of the cornerstones of OOP.
In addition to the private access modifier, there’s also a public access modifier. Given the stan- dard definitions of the words “public” and “private,” you can probably surmise that public members are easier to access than private members. If you declare a member to be public, then the member can be accessed from anywhere (from within the member’s class, and also from outside the member’s class). You should declare a method to be public when you want it to be a portal through which the outside world accesses your objects’ data. Go back and verify that all three methods in the Mouse class use the public access modifier. When you want a method to help perform a local task only, you should declare it to be private, but we’ll delay that consideration until Chapter 8.
Look once again at the Mouse class’s instance variable declarations. Note that we initialize age and weight to 0 and 1.0, respectively, but we don’t initialize percentGrowthRate. That’s because we’re comfortable with age = 0 and weight = 1.0 for all newborn Mouse objects, but we’re not comfort- able with a predefined initial vaAluepfoar pgeorcenPtGDroFwthERanteh. Parensumcaeblyr, we’ll want to use different percentGrowthRate values for different Mouse objects (mice in a doughnut-eating study might have higher percentGrowthRate values than mice in a cigarette-smoking study).
With no initialization for the percentGrowthRate instance variable, how can you set the growth rate for a Mouse object? You can have the Mouse object call the setPercentGrowthRate method with a growth rate value as an argument. For example, here’s how a Mouse object can set its growth rate to 10 (percent):
setPercentGrowthRate(10);
As you may recall from Chapter 5, a method call’s parenthetical values are referred to as arguments. Thus, in this example, 10 is an argument. The 10 gets passed into the percentGrowthRate variable in setPercentGrowthRate’s heading. A method heading’s parenthetical variables are referred to as parameters. Thus, in the example shown in Figure 6.4, percentGrowthRate is a parameter. Within the setPercentGrowthRate method body (the code between the method’s opening and closing braces), the percentGrowthRate parameter is assigned into the percentGrowthRate instance variable. Here’s the relevant assignment statement:
this.percentGrowthRate = percentGrowthRate;
Note the “this dot” in this.percentGrowthRate. The this dot is how you tell the Java compiler that the variable you’re referring to is an instance variable. Since the percentGrowthRate variable at the right does not have this dot, the Java compiler knows that that percentGrowthRate refers to the percentGrowthRate parameter, not the percentGrowthRate instance variable. In Figure 6.4’s setPercentGrowthRate method, the instance variable and the parameter have the same name. That’s a common practice. There’s no problem distinguishing between the two variables because the instance vari- able uses this dot and the parameter does not.