
Specifying a return value
for a constructor will cause
a compiler error in the
client program when the
client attempts to instanti-
ate an object of that class.
COMMON ERROR
TRAP
382 CHAPTER 7 Object-Oriented Programming,Part 2: User-Defined Classes
classes in more depth. It is also good practice to provide another construc-
tor that accepts values for all the instance variables.
On line 17, we instantiate an Auto object with an invalid argument for gallons
of gas. As Figure 7.1 shows, the constructor prints an error message. The object
is still created, but the value of its gallonsOfGas instance variable is 0.0.
Beware of this common error: declaring a ...