 Welcome to instantiation constructing an object. In this learning activity, the C-Sharp programming language is used to demonstrate how object constructors are used during instantiation. As an object-oriented programmer, it is important to understand how objects are created from classes. Object construction is one of the fundamental concepts in object-oriented programming. Knowing what is happening under the hood will help you avoid bugs and give you more options to control how objects are created. In object-oriented programming, or OOP, an object is an instance of a class. The class is defined in code. Programmers use the new keyword to create an object in memory from that class. This process is called instantiation. You could think of it this way. The new keyword is what allows an object to be born. Let's use an example from a class in the system.text namespace within the .NET framework. We will be creating an instance of the string builder class. To instantiate a string builder object, we first declare the data type of the object, string builder. Then we give a name to the instance we want to create, message builder. What we have created is really just a variable. The difference between declaring a variable to hold an object and declaring a variable to hold a primitive value, like int or bool, is important, but the concept is the same. At this point, we have created the variable, but it has yet to be assigned a value. Unlike primitives, if a variable is not assigned a specific value, no default value is assigned. The value does not exist, or in other words, it is null. We have assigned a placeholder in memory for our object to live, but it hasn't been born yet. Let's take a closer look at what happens under the hood. In languages like C sharp and Java, the memory the code uses when the application is running is organized into separate areas, the stack and the heap. There is a lot to learn about the stack and the heap, but in this activity, it is enough to know that the memory our application uses is divided into these two areas. When we declared the variable message builder, a space on the stack was reserved to hold that variable. What's actually stored there is called a reference. At this point, it is a reference to nothing. We call that a null reference. Let's finish the statement that will bring our string builder object to life. It is the new keyword that instructs the computer to create the object. In fact, programmers sometimes use the phrase newing up an object to mean the same thing as instantiation. When this code is executed, a value will be assigned to the message builder variable. Space to hold the string builder object will be reserved in memory on the heap. We said that the message builder variable is actually a reference held on the stack. Now that we have instantiated the object on the heap, the reference on the stack points to the memory space reserved on the heap. For reasons beyond the scope of this activity, it is necessary for objects to be created in this manner. At this point, we only need to know that message builder is a reference variable on the stack. This reference variable points to an instance of the string builder object on the heap. Now that we know how objects are instantiated, let's look more closely at the new keyword and how it works. All objects have a special method called a constructor. The constructor method is automatically called whenever we use the new keyword to instantiate an object. In C-sharp, the constructor method is a method with no return type and must be named exactly as the class name. The default constructor for string builder looks like this. In the case of the string builder, let's assume the default constructor doesn't actually do anything. Every object has a constructor, but in cases where the constructor doesn't do anything special, we call it an empty constructor. The programmer doesn't actually have to write it. To make programming time more efficient, the compiler will create an invisible empty constructor for you. Constructors are really just methods in behave like any other function except for one important difference. With other methods, we have to write code that explicitly calls the function. Constructors are special methods that are called when an object is instantiated. The new keyword is the trigger that calls the constructor method. Programmers typically write a constructor in a class if there is any work that needs to be done in order to set the object up for use. Like any other function, multiple versions of a constructor can be created. We call the act of creating multiple versions of a function overloading. Constructors are often overloaded in order to give programmers options for how a class can be used to create objects. The string builder class constructor has five overloads or five options for an object to be instantiated. Each of these options allow the programmer to pass specific information into the object to control how it is created or how it should behave. Depending on the specific needs of the class the programmer is designing, the ability to create constructors to do internal setup work is one of the most basic techniques. The ability to offer flexible options for object construction through overloading is also quite useful and is common within the .NET framework. Now it's time to check your understanding by answering the next four questions. What part of the statement triggers the constructor? What is the part of the statement that indicates the data type? What is the part of the statement that indicates the name of the variable? What is the part of the statement that completes the instantiation? In this activity, we defined instantiation as the process of creating an object from a class using the new keyword. We also looked under the hood to see how special methods known as constructors are automatically called whenever an object is instantiated. You have completed instantiation, constructing an object.