If the designer declares such instance variables
and methods to be public, other programs and objects can access them. On the other hand, the designer can also
give a class private instance variables and methods, and such private ???members??? enable the encapsulation virtue
of OO programming. Private variables and methods are not even visible outside the object.
In addition to instance variables and methods, classes can have static variables and static methods. Whereas
instance members (variables and methods) are associated with individual objects, static members are associated
with a class as a whole. Suppose, for example, that the class automobile includes a static variable called
countOfCars. Each time an automobile is created, the static variable countOfCars gets incremented. The
count, because it is a static variable, is associated with the class, not with any individual automobile. Assuming
the static variable countOfCars is public, any object can discover the number of automobiles created so
52 SOFTWARE [CHAP. 4
far by reading the value of Automobile.countOfCars. The dot notation says to return the value of
countOfCars from the Automobile class.
The most common use of static variables is for class constants.
Pages:
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153