Constructors are used to initialize the member variables of a class. When an object is created, memory is allocated for the object and the constructor for the class is executed. This typically occurs using the new
keyword.
Initialization of an object's instance variables is important. One of the developer's responsibilities is making sure that the state of an object is always valid. To assist in this process, constructors are executed whenever an object is created.
An alternate approach, which is not used by Java, is to use an initialization method that the programmer should call after an object is created. However, the use of such an initialization method is not a foolproof technique. The programmer may not be aware that the method exists, or may forget to call the method. To avoid these types of problems, a constructor is automatically invoked when an object is created.
The important characteristics of constructors include:
Constructors have the same name as the class
Constructor...