In the previous episodes, we have learnt how to declare classes and objects, that is, class instances. For example, in order to declare a "rect" instance of the "Rectangle" class, we will write "Rectangle rect; ". Now, we could wonder how to give other values for the default variables (which is 0) to the attributes of our rect instance. For example, the attributes "hauteur" and "largeur" (TN: "hauteur" means "height", "largeur" means "width"). Even better, we would like to initialize, namely give an initial value of "rect"'s creation, to the "hauteur" and "largeur" attributes. Of course, we could resort to the setter "setHauteur" and "setLargeur", thus assigning individually, one after the other, values for each attribute. For example, here, we have declared a "rect" instance of the "Rectangle" class. We have prevously declared a double "lu" and ask the user to enter the height on the keyboard. Then, through the setter "setHateur", we will assign the value read from the keyboard in "lu" to the height of the "rect" instance. Similarly, via user interaction, we will assign the value of "lu" to the width of the "rect" instance. Obviously, this is a very poor solution in the general case. Indeed, this will either require for each attribute to be part of the interface (which will break the encapsulation as we have seen in last week's episodea) or for us to have a setter for attribute. This itself translates how the class has been implemented and breaks the encapsulation since we need a setter for each attribute. The goal of this encapsulation is precisely to clearly separate the interface from the implementation. If we had, for each attribute, a setter in the interface, the implementation choices would spill over the inteface. In that way does it break the encapsulation. Moreover, we are bestowing the responsability to initialize the attributes on the programmer-user of the class. This begets the risk of omissions. It is the duty of the programmer-developper to initialize the attributes or offer an interface permitting the initialization of a few carefully selected attributes for the programmer-user of the class. So far, every time we have indentified a specific task, we created a specific method dedicated to this task. Right now, this task is the initialization. A solution is thus to create a method dedicated to the initialization. For example, an "init" method, here. Of course, we will offer it in the interface so that programmer-users of the class may use it. This "init" method will thus be tasked to initialized the two attributes "hauteur" and "largeur". To that end, it will receive two values "h" and "l" to initialize the height and the width respectively. Typically, we would use this newly- created initialization method like this. We will declare a "rect" instance of the "Rectangle" class. Then, we will call the "init" method in order to initialize, for example, the height to 3 and the width to 4. This is definitely not a poor solution. Actually, this is such a good solution that the Java language offers a way to do so properly. Namely, letting us do a true initialization. Such methods, tasked with the instance initialization are called constructors. A constructor is a method which is systematically called upon an object's declaration. In other words, it is called when an instance is born. Its task is thus to do whatever is necessary for the beginning of an instance's life -particularly initializing its attributes. In order to declare a constructor, we simply need to add a method which name is none other than the name of the class. Then, follow the possible parameters required for the initialization. As usual, the definition of the metod then follows. For example, for the "Rectangle" class, we will define a "Rectangle" method (the same is the same as the class) which is thus a constructor. It receives two parameters "h" and "l" in order to initialize, in the body of this method, the attributes "hauteur" and "largeur" respectively. Constructors are very similar to your average methods. There are however three differences! First of all, a constructor has no return type. We have no written anything here, not even "void"; this means that the constructor has no return type at all. The second difference is that the constructor MUST have a name identical to the name of the class. Finally, a constructor will systematically be called every time a new instance is created. It is called with a syntax we will detail in an instant. These are the differences. However, constructors can, like other methods, be overloaded. This means that we will have several different parameter lists for different constructors. We may thus have several constructors. We will go back to this point later. For now, we merely wish to stretch that we talking about the possible constructorS for a class. There is not necessarily a unique constructor. Let us go back to our starting example and see how this is concretely written. We will declare only one constructor in our "Rectangle" class. To that end, we will declare the "Rectangle" class as usual. Its attributes "hauteur" and "largeur" are in the private parts. As usual, the different setters, getters and methods we have decided to put in the interface. However, we have now added a constructor. This constructor is a method with the same name as the class. Of course, this constructor must be part of the interface since its goal is to be called by the people using the class. It needs to be accessible from outiside the class. In this case, it receives two parameter: "h" and "l" in order to initialize the "hauteur" attribute and the "largeur" attribute. Let us now see a constructor is called. That is, how will we declare an instance which we will declare an initialize at the same time. To that end, we will resort to a usual declaration of a certain class' instance here. This declaration will be followed by " = new ", name of the class. This here, will be a call to the constructor to which we pass, between parentheses, the different parameters necessary for the constructor. Here, in the example of our "Rectangle" clsss, we will declare an "r1" instance of the "Rectangle" class, " = new ", call to the "Rectangle" constructor which takes two parameters here. Concretely, in this example we are following from the start, we thus have the "Rectangle" class with, here, in public, a constructor taking two parameters. It will be responsible to initialize the parameters "largeur" and "hauteur" of the class. For example, here, in the class "Example" where we have the "main" we will declare an "rect1" instance of the "Rectangle" class. We initialize it directly through a call to the constructor. We pass the value 3 to the parameter "h" in order to store it in "hauteur" and the value 4 to the parameter "l" in order to store it in the "largeur" of "rect1". Concretely, the following will happen in the memory. "rect1" is thus a reference on a rectangle. Upon this "new Rectangle" instruction, we will indeed create an "Rectangle" instance in the memory with these two parameter "hauteur" and "largeur". The call to the constructor, here, will thus initialize these to the values 3 and 4. Voilà ! This concludes our introductory sequence on the constructors. We still have a lot to tell you, though. For example, we will discuss the default constructor, which is unique. We will discuss the copy constructor and see what happens at the end of an object's life. And we will see how to compare different objects. This will be the subject of the following episodes.