THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Some classes always require that the creator supply certain kinds of data. For example, your application might
require that all Body objects have a name. To ensure that all statements creating Body objects supply a name,
you would define all Body constructors with a name parameter and you wouldn't bother initializing the name
field.


Here are some common reasons for providing specialized constructors:



  • Some classes have no reasonable initial state without parameters.
    Providing an initial state is convenient and reasonable when you're constructing some kinds of objects
    (the two-argument constructor of Body is an example).



Constructing an object can be a potentially expensive operation, so you want objects to have a correct
initial state when they're created. For example, if each object of a class had a table, a constructor to
specify the initial size would enable the object to create the table with the right size from the
beginning instead of creating a table of a default size, which would later be discarded when the
method that set the actual size was invoked.


A constructor that isn't public restricts who can create objects using it. You could, for example,
prevent programmers using your package from creating instances of a class by making all its
constructors accessible only inside the package.


Constructors without arguments are so common that there is a term for them: no-arg (for "no arguments")
constructors. If you don't provide any constructors of any kind in a class, the language provides a default
no-arg constructor that does nothing. This constructorcalled the default constructoris provided automatically
only if no other constructors exist because there are classes for which a no-arg constructor would be incorrect
(like the Attr class you will see in the next chapter). If you want both a no-arg constructor and one or more
constructors with arguments, you must explicitly provide a no-arg constructor. The default constructor has the
same accessibility as the class for which it was definedif the class is public then the default constructor is
public.


Another form of constructor is a copy constructorthis constructor takes an argument of the current object type
and constructs the new object to be a copy of the passed in object. Usually this is simply a matter of assigning
the same values to all fields, but sometimes the semantics of a class dictate more sophisticated actions. Here is
a simple copy constructor for Body:


Body(Body other) {
idNum = other.idNum;
name = other.name;
orbits = other.orbits;
}


Whether this is correct depends on the contract of the class and on what the uniqueness of idNum is supposed
to indicatefor this example we won't concern ourselves with that.


This idiom is not used much within the Java class libraries, because the preferred way to make a direct copy
of an object is by using the clone methodsee "Cloning Objects" on page 101. However, many classes
support a more general form of construction that "copies" another object. For example, the
StringBuilder and StringBuffer classes (described in Chapter 13) each have a constructor that takes
a single CharSequence argument that allows you to copy an existing String, StringBuffer, or
StringBuilder object; and the collection classes (described in Chapter 21) each have a constructor that
takes another Collection as an argument and so allow one collection to be initialized with the same
contents as another (which need not be of exactly the same type). Writing a correct copy constructor requires
the same consideration as writing a correct clone method.


Constructors can also be declared to throw checked exceptions. The tHRows clause comes after the

Free download pdf