Java Reference
In-Depth Information
Because neither the Person class nor the Bid class initiates any activity within the auction sys-
tem, we shall not discuss them here in detail, and so studying these classes is left as an exercise
to the reader. Instead, we shall focus on the source code of the Lot and Auction classes.
4.14.2 The null keyword
From the discussion above, it should be clear that a Bid object is only created when someone
actually makes a bid for a Lot . The newly created Bid object then stores the Person making
the bid. This means that the Person field of every Bid object can be initialized in the Bid con-
structor, and the field will always contain a valid Person object.
The Java reserved
word null is
used to mean “no
object” when an
object variable is
not currently refer-
ring to a particular
object. A field that
has not explicitly
been initialized will
contain the value
null by default.
In contrast, when a Lot object is created, this simply means it has been entered into the auction
and it has no bidders yet. Nevertheless, it still has a Bid field, highestBid , for recording the
highest bid for the lot. What value should be used to initialize this field in the Lot constructor?
What we need is a value for the field that makes it clear that there is currently “no object” be-
ing referred to by that variable. In some sense, the variable is “empty.” To indicate this, Java
provides the null keyword. Hence, the constructor of Lot has the following statement in it:
highestBid = null;
A very important principle is that, if a variable contains the null value, a method call should not be
made on it. The reason for this should be clear: as methods belong to objects, we cannot call a method
if the variable does not refer to an object. This means that we sometimes have to use an if statement
to test whether a variable contains null or not before calling a method on that variable. Failure to
make this test will lead to the very common runtime error called a NullPointerException . You
will see some examples of this test in both the Lot and Auction classes.
In fact, if we fail to initialize an object-type field, it will be given the value null automatically.
In this particular case, however, we prefer to make the assignment explicitly so that there is no
doubt in the mind of the reader of the code that we expect highestBid to be null when a Lot
object is created.
4.14.3 The Lot class
The Lot class stores a description of the lot, a lot number, and details of the highest bid re-
ceived for it so far. The most complex part of the class is the bidFor method (Code 4.9). This
deals with what happens when a person makes a bid for the lot. When a bid is made, it is neces-
sary to check that the new bid is higher in value than any existing bid on that lot. If it is higher,
then the new bid will be stored as the current highest bid within the lot.
Code 4.9
Handle a bid for a lot
public class Lot
// The current highest bid for this lot.
private Bid highestBid;
Other fields and constructor omitted.
Search WWH ::

Custom Search