Java Reference
In-Depth Information
that receives three
int
parameters representing the
hour
,
minute
and
second
. This con-
structor validates and initializes the instance variables.
Lines 47-51 declare a
Time2
constructor that receives a reference to another
Time2
object. The values from the
Time2
argument are passed to the three-argument constructor
at lines 30-44 to initialize the
hour
,
minute
and
second
. Line 50 could have directly
accessed the
hour
,
minute
and
second
values of the argument
time
with the expressions
time.hour
,
time.minute
and
time.second
—even though
hour
,
minute
and
second
are
declared as
private
variables of class
Time2
. This is due to a special relationship between
objects of the same class. We'll see in a moment why it's preferable to use the
get
methods.
Software Engineering Observation 8.5
When one object of a class has a reference to another object of the same class, the first object
can access
all
the second object's data and methods (including those that are
private
).
Class
Time2
's
setTime
Method
Method
setTime
(lines 56-70) throws an
IllegalArgumentException
(lines 59, 62 and
65) if any the method's arguments is out of range. Otherwise, it sets
Time2
's instance vari-
ables to the argument values (lines 67-69).
Notes Regarding Class
Time2
's
set
and
get
Methods and Constructors
Time2
's
get
methods are called throughout the class. In particular, methods
toUniversal-
String
and
toString
call methods
getHour
,
getMinute
and
getSecond
in line 122 and lines
129-130, respectively. In each case, these methods could have accessed the class's private
data directly without calling the
get
methods. However, consider changing the representation
of the time from three
int
values (requiring 12 bytes of memory) to a single
int
value rep-
resenting the total number of seconds that have elapsed since midnight (requiring only four
bytes of memory). If we made such a change, only the bodies of the methods that access the
private
data directly would need to change—in particular, the three-argument constructor,
the
setTime
method and the individual
set
and
get
methods for the
hour
,
minute
and
second
.
There would be no need to modify the bodies of methods
toUniversalString
or
toString
because they do
not
access the data directly. Designing the class in this manner reduces the
likelihood of programming errors when altering the class's implementation.
Similarly, each
Time2
constructor could include a copy of the appropriate statements
from the three-argument constructor. Doing so may be slightly more efficient, because the
extra constructor calls are eliminated. But,
duplicating
statements makes changing the class's
internal data representation more difficult. Having the
Time2
constructors call the con-
structor with three arguments requires that any changes to the implementation of the three-
argument constructor be made only once. Also, the compiler can optimize programs by
removing calls to simple methods and replacing them with the expanded code of their dec-
larations—a technique known as
inlining the code
, which improves program performance.
Using Class
Time2
's Overloaded Constructors
Class
Time2Test
(Fig. 8.6) invokes the overloaded
Time2
constructors (lines 8-12 and 24).
Line 8 invokes the
Time2
no-argument constructor. Lines 9-12 demonstrate passing argu-
ments to the other
Time2
constructors. Line 9 invokes the single-argument constructor that
receives an
int
at lines 18-21 of Fig. 8.5. Line 10 invokes the two-argument constructor at
lines 24-27 of Fig. 8.5. Line 11 invokes the three-argument constructor at lines 30-44 of