Java Reference
In-Depth Information
The definition of the copy constructor for the
class
Clock
is:
public
Clock(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
If you include this definition of the copy constructor in the
class
Clock
, then the
statement in Line 2 declares
aClock
to be a reference variable of type
Clock
, instantiates
the object
aClock
, and initializes the instance variables of the object
aClock
using the
values of the instance variables of the object
myClock
.
The definition of the copy constructor of the
class
Clock
can also be written as:
public
Clock(Clock otherClock)
{
setTime(otherClock.hr, otherClock.min, otherClock.sec);
}
The copy constructor is useful and will be included in most of the classes.
8
In Chapter 7, we described the
class
es
Math
and
Character
. In Example 7-1 (of
Chapter 7), we used several methods of the
class
es
Math
and
Character
; however, we
did not need to create any objects to use these methods. We simply used the import statement:
import static
java.lang.Math.*;
and then called the method with an appropriate actual parameter list. For example, to use
the method
pow
of the
class
Math
, we used expressions such as:
pow(5, 3)
Recall from Chapter 7 that if you are using versions of Java lower than Java 5.0 or you do
not include the preceding
import
statement, then you call the method
pow
as follows:
Math.pow(5, 3)
That is, we can simply call the method using the name of the class and the dot operator.
We cannot use the same approach with the
class
Clock
. Although the methods of the
class
Math
are
public
, they also are defined using the modifier
static
. For example,
the heading of the method
pow
of the
class
Math
is:
public static double
pow(
double
base,
double
exponent)
The modifier
static
in the heading specifies that the method can be invoked by using
the name of the
class
. Similarly, if a data member of a
class
is declared using the
modifier
static
, it can be accessed by using the name of the
class
.
Search WWH ::
Custom Search