Java Reference
In-Depth Information
TIP:
(continued)
A no-argument constructor is also known as a
default constructor
. However, the term
default constructor
is misleading since, as we have explained, a no-argument constructor
is not always provided by default. There is now a movement to replace the term
default
constructor
with the term
no-argument constructor,
but you will frequently encounter
the term
default constructor.
default
constructor
■
No-Argument Constructor
A constructor with no parameters is called a
no-argument constructor
. If your class defini-
tion contains absolutely no constructor definitions, then Java automatically creates a no-
argument constructor. If your class definition contains one or more constructor definitions,
then Java does not automatically generate any constructor; in this case, what you define is
what you get. Most of the classes you define should include a definition of a no-argument
constructor.
EXAMPLE:
The Final Date Class
The final version of our class for a date is given in Display 4.13. We will be using this class
Date
again in Chapter 5.
Self-Test Exercises
24. If a class is named
CoolClass
, what names are allowed as names for constructors
in the class
CoolClass
?
25. Suppose you have defined a class like the following for use in a program:
public class
YourClass
{
private
int
information;
private
char
moreInformation;
public
YourClass(
int
newInfo,
char
moreNewInfo)
{
<
Details not shown.
>
}
public
YourClass()
{