hey everybody welcome back so in today's video we're going to discuss constructors a constructor is a special method within a class that is automatically called when an object is instantiated it's useful for assigning values to attributes as arguments before when we were assigning attributes to an object we would say for example car one dot make equals forward the constructor we can do that automatically here's an example this time let's create a student class class student i'll add a public access modifier what attributes should students have how about a name standard string name int age double gpa sounds good to me then if i need to create a student object i would type the name of the class student a unique identifier for this specific object let's say student1 then to assign some of the attributes right away i could use a constructor we do have a constructor that is automatically called behind the scenes but we could explicitly set one up the constructor has the same name as the class in this case student add a set of parentheses add a set of curly braces think of this as a function we can set up parameters when we instantiate a student object we will automatically call this function the constructor then pass arguments let's set up some parameters we have standard string name int age double gpa then when i instantiate a student object i will add a set of parentheses after the object name then pass in my arguments let me just zoom in real quick okay in order to instantiate a student object we need to pass in a name an age and a gpa so for the first student student one's name will be spongebob his age will be i don't know how old spongebob is let's say he's 25. spongebob's gpa will be a solid 3. 2 when we instantiate our student object we will pass these values as arguments to the constructor now to assign these attributes with these parameters we first need to select these attributes in this example i gave them the same name just to remove ambiguity if i'm referring to these attributes i will type this then an arrow name equals the name of the parameter name this name equals my name parameter this arrow age equals my age parameter this arrow gpa equals my gpa so now let's test this i will display student one's name age and gpa standard output student1 dot name add a new line then we have age then gpa okay we got spongebob he's 25 years old his gpa is 3.
2 so you can see that we don't need to necessarily add these values to these attributes manually you can do that automatically with the constructor so another way in which you may see this constructor set up is with the parameter names being different from the attribute names perhaps instead of name age and gpa let's say x y then z if the attribute names are different from the parameter names you don't need the this keyword you could say name equals x age equals y gpa equals z and this would work too if you prefer this way of doing things you can do that uh just my own personal preference i like to use the this keyword and then keep my parameters the same but that's just me you do you let's create a couple more students and it's kind of nice because we can reuse this constructor let's create student two student student two student two will be patrick i don't know how old patrick is let's say patrick is 40. uh patrick's gpa will be 1. 5 okay to test this let's display student twos and name age and gpa okay we got patrick he's 40 years old gpa is 1.
5 one last student student student three student three will be sandy sandy is uh how about 21 years old and sandy's gpa is a perfect 4. 0 okay now we'll display student three's name age gpa okay we got sandy her age is 21 gpa is for well 4.