Java Reference
In-Depth Information
ArrayList vs. LinkedList
ArrayList typically yields faster access than LinkedList. With an
ArrayList, accessing any element requires the same time regardless
of the size of the array.
In Figure 9-5 on page 547, line 23 declared an instance variable with a type of
ArrayList. To create an ArrayList object, you need to call the appropriate con-
structor using the new keyword, as you would with any other object, and assign it
to the instance variable. This constructor is listed in the SDK documentation as
ArrayList(int initialCapacity)
When constructed, the ArrayList() method has one parameter: an int value that
represents the initial capacity of the list. As you will see, this ArrayList construc-
tor will be called in each of the constructors of the Password class.
Creating Overloaded Constructors
You may remember that a constructor is called automatically when you
create a new instance of a class (that is, an object). Recall that a constructor is a
special kind of instance method that has the same name as the class and has no
return data type, not even void; further, a constructor may or may not have a
parameter list. A Java constructor that has no parameters (and therefore requires
no arguments to be passed when called) is termed a default constructor . If a
class has no constructors coded, then the compiler creates a default constructor
that serves only as a target for the operator, new, which causes a constructor to
be called. If even a single constructor is coded, then the compiler will not create
a default constructor; in such cases, if the class is to have a default constructor,
the programmer then must code it.
The major function of a constructor is to ensure that all instance variables of
the class are initialized appropriately. The variables can be initialized by directly
assigning a value, by allocating resources such as new instances of other objects,
or by calling other methods within the class to establish a valid value. When a
constructor is called with an empty argument list it uses default values for all
instance variables. A default value is the value used when no other value is sup-
plied. The values assigned to each of the instance variables coded in lines 18
through 21 are the initial, or default, values that are used unless replaced with a
new value in a constructor.
If no constructors were coded, the default values always would be the initial
values for these variables each time an object was created. Depending on the cir-
cumstances, you may want different initial values for different objects. Further-
more, when an instance variable is a reference to another object, as is the
pswdHistory variable in line 23, that other object must be constructed for the
reference variable to have an initial value other than null — a process typically
performed by the constructor.
The requirements document states that the Password class should support
only constructors which are supplied, at a minimum, with a password value.
Therefore, this class will not support a default constructor. Additionally, the
Search WWH ::




Custom Search