Java Reference
In-Depth Information
for the parameter variables is created and the external values copied into that space. Once that
call has completed its task, the formal parameters disappear and the values they held are lost.
In other words, when the constructor has finished executing, the whole constructor space (see
Figure 2.4) is removed, along with the parameter variables held within it.
In contrast, the lifetime of a field is the same as the lifetime of the object to which it belongs.
When an object is created, so are the fields, and they persist for the lifetime of the object. It fol-
lows that if we want to remember the cost of tickets held in the cost parameter, we must store
the value somewhere persistent—that is, in the price field.
Just as we expect to see a close link between a constructor and the fields of its class, we expect
to see a close link between the constructor's parameters and the fields, because external values
will often be needed to set the initial values of one or more of those fields. Where this is the
case, the parameter types will closely match the types of the corresponding fields.
Exercise 2.18 To what class does the following constructor belong?
public Student(String name)
Exercise 2.19 How many parameters does the following constructor have, and what are
their types?
public Book(String title, double price)
Exercise 2.20 Can you guess what types some of the Book class's fields might be, from
the parameters in its constructor? Can you assume anything about the names of its fields?
2.5.1
Choosing variable names
One of the things you might have noticed is that the variable names we use for fields and
parameters have a close connection with the purpose of the variable. Names such as price ,
cost , title , and alive all tell you something useful about the information being stored
in that variable. This, then, makes it easier to understand what is going on in the program.
Given that we have a large degree of freedom in our choice of variable names, it is worth
following this principle of choosing names that communicate a sense of purpose rather than
arbitrary and meaningless combinations of letters and numbers.
2.6
Assignment
In the previous section, we noted the need to store the short-lived value stored in a param-
eter variable into somewhere more permanent—a field variable. In order to do this, the body
of the constructor contains the following assignment statement:
Concept:
Assignment state-
ments store the
value represented
by the right-hand
side of the state-
ment in the variable
named on the left.
price = cost;
Assignment statements are used a lot in programming, as a means to store a value into a
variable. They can be recognized by the presence of an assignment operator, such as “=” in the
example above. Assignment statements work by taking the value of what appears on the right-
hand side of the operator and copying that value into the variable on the left-hand side. This is
 
 
Search WWH ::




Custom Search