In this new episode, we will focus on the default constructor. Namely the constructor which are called when we do not provide any initialization value, no parameter. We will also discuss what happens if, in a classe, we do not define any explicit constrcutor. A default constructor is simply a constructor which has no parameter. You can see here a concrete example. We can imagine that, in the "Rectangle", three different constructors cohabit. This means, three different ways to initialize a "Rectangle"-type example. The default constructor -without parameter- has for mission to initialize the "hauteur" and "largeur" attributes to default values. Concretely, this default constructor will be summoned like this. We will declare an "r" object of the "Rectangle" type. We will initialize it by using the default constructor. It will be call through this instruction. Here, since we are specifying that the constructor is called with an empty argument list, this means we are indeed calling this constructor here. After this initialization, the situation in the memory will be this one : We will thus have an "r" variable containing a reference on a "Rectangle"-type object. Its "hauteur" member will be initialized to the default value 1.0 and its "largeur" member to the default value 2.0 The other constructors of the class are not default constructors; indeed, their parameter lists are not empty. If, for example, we wish to summon this constructor here, we must use this particular phrase. Thus, just like before, we declare a "Rectangle"-type variable "r1". We call the constructor will a phrase similar to the one we have seen previously. However, we precise, as argument, the value we wish to give to this parameter "c". Now, what does happen if the programmer of a class does not code any explicit constructor? Why, the object initialization is considered so essential that a constructor will be automatically generated for this call. This will obviously be a default constructor. We will call it the default default constructor. This automatically-generated constructor is a default constructor because it has not parameter. We call it "default" itself because it is provided automatically if there is no other explicit constructor in the class. This default default constructor will do a minimum work regarding the initialization. It will initialize all basic-type attributes with default values such as 0 , 0.0 or "false". Regarding objects, they will all be initialized to "null". Let us suppose that we define a "Rectangle" class. It will have, as in our previously-seen examples, two double-type attributes "largeur" and and "hauteur". It will also have another, more complex argument : for example, an object (an instance) of another class. For example a "Position" class permitting to represent the Rectangle's position on a two- dimensional screen. Thus, in the "Rectangle" class, there is no explicit constructor. That means that the default default constructor is generated; we can use it to initialize a rectangle. For example, in another part of the program, we may declare a "Rectangle"-type variable, a "Rectangle" object and initialize it with the default default constructor which, of course, does not take any parameter. After this initializaiton, we will have, in the "rect" variable, a reference on a "Rectangle"-type object. Its height and width will be initialized to default values since they are basic-type attributes. The height and width will thus have the value 0.0 Also, the position, the "position" attribute, will be initialized to "null": since it is an object. By the way, in order not to overburder the writing of this example, we have not precised the access rights. Rigorously, for the better encapsulation, all attributes should be declared as "private". Be careful though! This default default constructor is not provided anymore as soon as we define at least one explicit constructor for this class; be this constructor default or not. Concretely, this means that if, in a class, we specify a constructor which is not a default constructor, we have no way to build a object without providing initialization values. In other words, we cannot write something like this anymore. This instruction, calling a default constructor is not valid anymore. Why? Because the default default constructor has disappeared. This corresponds to something we could wish. If we have taken the time to explicitly program a constructor in the class, we do not wish for Java to inconspicuously slide a default constructor we never asked for; we never explicitly specified we wished to use a default constructor. In order to illustrate this, we will now study three variants of the same program differing one from the others only by the constructors. In the first variant, the A variant, a "Rectangle" class is defined with two double-type attributes "hauteur" and lsrgeur". This class can have more content but it has no explicit constructor at all. In the B variant, there are still the same attributes. This time though, we have an explicit constructor which is a default constructor because its parameter list is empty. This constructor will initialize the height and the width to the value 0.0 Finally, in the C variant, the "Rectangle" class has only one explicit constructor which is not a default construcotr anymore since its parameter list is not empty. Here, this constructor expects one value for the height and another for the width. The constructor will then assign these values to the corresponding attributes. For each of these variants, we will ask ourselves the following questions: Is there a default constructor? Is it valid to initialize an object with this instruction? Is it valid to initialize an object with this other instruction? Let us begin by the first variant, the A variant. Remember that, here, we are only indicating the constructors of the class; not the rest. Here, we do not have any explicit constructor. As we have seen previously, when a class does not define any explicit constructor, a default constructor is generated. It is the default default constructor. Therefore, the initialization through this instruction is valid. We are using a constructor without parameter. We have seen that, for basic-type attributes, the default default constructor initialize these attributes to the basic values. Thus, the height will be 0.0 and the height will be 0.0 However, since the only available constructor is the default default constructor, we cannot initialize an object with this instruction. Since we are using a constructor without parameter, this instruction expecting parameters is invalid. Regarding variant B, we have, in the "Rectangle" class an explicit constructor; it is a default constructor. On the question asking if there is a default constructor, the answer is yes. It is this explicitly declared constructor. Just like before, we can obviously use this default constructor with this instuction, calling a constructor expecting no parameter. Here, this constructor will have the same effect as the the default default constructor. Indeed, it initializes the height and the width to the same values as the ones used by the default default constructor. On the same basic as for variant A, this instruction is invalid; there is indeed no other constructor as the default constructor in the "Rectangle" thus. Therefore, no constructor expecting two values could work here. Finally, for the C variant, we have an explicit constructor which is not a default constructor. On the question asking if there is a default constructor, the answer is no. There is no default constructor. Why? As we have said before, as soon as we define an explicit constructor -be it a default constructor or not- the default default constructor vanishes. Therefore, the only constructor available in this class is now this one. It is thus not possible to initialize an object with this instruction. There is no constructor which does not expect any argument. This is thus invalid. On the other hand, the second instruction becomes valid. It will initialize the height and the width with the values passed as argument. We will thus obtain a "Rectangle" object where the height will be 1.0 and the width 2.0 Let us now examine a few shortcuts provided by the Java language regarding object construction and attribute initialization. If there are several constructors in a class, Java lets any constructor summon any other constructor of the same class. This is done with a particular instruction : "this ". We are thus using the reserved keyword "this" and, between parentheses, we provide the arguments corresponding to those expected by the constructor we wish to call. For example, in this "Rectangle" class, we have an explicit constructor with two parameters. We can define the default constructor using this first definition of the constructor. Here, the default constructor, through this "this" instruction, summons the constructor expecting two parameters -this one- providing, for the first parameter, the value 0.0 and 0.0 aswell for the second parameter. This is an elegant twist permitting, in many a case, to avoid duplicating code. But please note that there can only be one "this" instruction in a constructor. Also, this "this" instruction must be the very first summoned instruction. We cannot put any other instruction before the "this". It is also possible, in Java, to give default values to the attributes, without passing by the constructor. This is done upon the declaration of the attribute. Here, for example, when we declare the "hauteur" attribute, we give it a default value : 1.0 Now, we summon the default constructor of the class with such a familiar instruction. Like this. In the default constructor, there is no value explicitly assigned to "hauteur" or "largeur". Therefore, the default values will be taken. After the constructor, we will have an object in the memory. A "Rectangle"-type object. Its height has been initialized to the default value here : 1.0 The same goes for the width. Let us now imagine that the default constructor, instead of having an empty body, initializes only the width member to, for example, 3.0 After the constructor of a rectangle with this default constructor, we will have, in the memory, a "Rectangle"-type object. Of course, the "hauteur" member will have been initialized to this value 3.0 And since the "hauteur" member does not receive any explicit value in the constructor, it will take the default value specified upon the declaration of the attribute, that is, 1.0 Please note that, in order to make your programming intentions clear, it is recommanded to initialize the attributes in the constructors instead of resorting to this kind of twist. Indeed, if each of the constructors is tasked with all the necessary initializations, it is possible to understand what are the realized initializations merely upon the reading of these constructors. It is not necessary to search everywhere in the class for other implicit initializations. Voilà! We have no concluded this sequence on the default constructors. We will go back to them in later episodes on the inheritance. In the meantime, you will discover other possible ways to build an object through the copy constructors.