Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
The constructor is the first thing that runs when you create an object.
Having a constructor allows us to create the object.
And update its fields on the exact same line.
This is a big improvement on our previous code where we created the object and then in four separate
lines, we updated its fields.
This looks really disorganized.
So it stands to reason that in this lesson you will use a constructor to update an object as Fields.
The constructor runs as soon as you create an object.
And the purpose of a constructor is to update an object as Fields.
Let's go through the process.
Step one is to create a constructor that defines four parameters.
And to make a constructor, you need to specify the level of access which tends to be public.
The class name.
Followed by parameters.
In step two, you have to call the constructor while creating a new car object.
New car creates a new object of the car class.
And while creating the car object by passing in four arguments, you're calling the constructor that
expects to receive four parameters, and inside of the constructor is where you should update the newly
created objects as fields.
And that's why.
Step three is that the constructor must update the objects as fields.
The key word.
This refers to the current object.
The purpose of this is to distinguish between fields and parameters.
You'll notice here that the parameters have the exact same name as the object as Fields.
So we can use the this keyword to refer to the current object, the object that called the constructor.
And it follows that this make refers to the field in that object.
And we can set that field equal to the make parameter.
In turn, we can set every field equal to the parameter that was passed in.
And that's all.
Once the constructor is done updating the objects as fields, the object has been fully initialized
and our variable will store a reference to it.
All right, let's put all of this into code.
Step one was to create a constructor that defines four parameters.
The constructor will be public and the constructor will get called whenever we create objects of the
car class.
And whenever the constructor gets called, it's going to expect for parameters to be passed in.
It's going to expect a make a price.
A year.
And a color.
Step two is to call the constructor while creating a new car object.
So now that we've created our own constructor, Java is going to expect us to call it after we create
a new car object.
And we can call the constructor by passing in an equal number of arguments.
We will pass any make of Nissan a price of 10,000 a year of 2020 and a colour of green.
And now, instead of updating, the object is fields in four separate lines.
The constructor is going to do it in a single line.
And do the same thing over here.
After we create a new object of the car class, we want the constructor that expects four parameters
to get called.
So here we're going to pass in four values Dodge 11,000, 2019, and Blue.
And now, instead of updating, the object is fields in four separate lines.
The constructor is going to do it in a single line.
Looking good.
And now step three is for the constructor to update the objects as fields.
Every object that we define from the car class, it's going to have a field that matches a parameter
in the constructor.
So what we can do to distinguish between them is say this.
So this refers to the current object that's calling this constructor, which means that this make refers
to the field in that object.
And here we can set the current objects as field equal to them.
Make that gets passed in here, we can set the current object price field equal to the price value that
gets passed in here.
We can set the current objects year field equal to the year of value that gets passed in, and here
we can set the current object as color field.
Equal to the color value that we pass into our constructor.
And that's really it.
Whenever we create a new object of the car class, the constructor that expects to receive four values
is going to get called, and it's going to update every single field in that object.
This is looking really good.
Let us visualize the runtime to see this in action.
So here we're creating a new object of the car class.
If I press a step inside, it's going to step inside the constructor that expects to receive four values.
The make is a Nissan.
The price is 10,000.
The year is 2020 and the color is green.
You can also visualize the parameter values over here.
This points to the current object that we just created the new car object.
By default, all of the fields start at zero or null.
So inside the constructor we set every single field inside of the current object equal to a parameter.
The start make equals make.
This price equals the price value that we passed in.
This year equals the year value that we passed in.
And this color equals the color value that we passed in.
All right.
Once the constructor is done updating the object to Fields, the object has been fully initialized.
So now our variable Nissan is going to store a reference to it.
And now here, once again, we're creating a new object of the car class.
If I press step into, it's going to step inside the constructor that expects to receive four values.
This refers to the current object that we just created.
Here we can see the parameter values that were passed in.
Here we're setting this make the current objects is make field equal to the make value that we passed
in Dodge.
Here.
We're setting the price field of the current object.
This price equal to the price that we passed in 11,000.
Here we're setting the year field of the current object this year equal to the year value that we passed
in.
And here we're setting the color field of the current object.
Equal to the color that was passed in blue.
And once the constructor is done updating the current objects as fields, now the object has been fully
initialized and our dodge variable will store a reference to that object.
So now we've got two variables.
Each variable stores a reference that points to a brand new object.
And here we can print the field values of each object.
So our Nissan has a make of Nissan, a price of 10,000 a year of 2020 and a color of green.
That is exactly what should reflect in our print function.
Here we print every single field of the object that our Dodge variable points to.
So this object is going to have a make of dodge, a color of blue, a price of 11,000 and a year of
2019.
And that's really it in this lesson.
You used a constructor to update an object's fields.
New car creates a new object of the car class.
By passing in four values, we called the constructor that expects to receive four parameters.
This points to the current object, the newly created object that called the constructor.
So this dot make refers to the field in that object and we can set that field equal to the make parameter.
And we can set each field in the current object equal to the parameter that was passed in.
And that's really it.
Once the constructor is done updating the object as fields, the object has been fully initialized and
our variable will store a reference to it.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.