Every class must have at least one constructor—if you do not provide any constructors in a class’s declaration, the compiler creates a default constructor that takes no arguments when it’s invoked. In Section 11.4.1, you’ll learn that the default constructor implicitly performs a special task.
The compiler will not create a default constructor for a class that explicitly declares at least one constructor. In this case, if you want to be able to invoke the constructor with no arguments, you must declare a parameterless constructor—that is, one that’s declared with no parameters or one in which all the parameters have default values (e.g., line 12 of Fig. 10.5). Like a default constructor, a parameterless constructor is invoked with empty parentheses. If you call class Time2
’s three-argument constructor with no arguments, the compiler explicitly passes 0
to each parameter. If we omit from class Time2
a constructor that can be called with no arguments, clients of this class would not be able to create a Time2
object with the expression new
Time2()
. If a class provides both a parameterless constructor and a constructor with a default arguments for all of its parameters, the compiler will use the parameterless constructor when you pass no arguments to the constructor.