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
We need to fix our constructor so that it can update every single field, the car class, it's five
fields.
So when you create an object of the car class, the object is going to define Fifield's.
However, your constructor only updates four of them.
In this lesson, you're going to fix your constructor.
Your constructor is missing a parameter, it's the constructor's job to update every field in the object,
so your constructor needs to receive another parameter string parts.
And now set the array field from the current object.
This parts is equal to a copy of the perimeter erased that copy of.
We're going to get a copy of the Pozza ray.
And how much of it do we want to copy all of it parts just like remember to avoid setting array variables
equal to each other, otherwise you'll fall into a reference trap.
We talked about this table in Module one, but it's also in your cheat sheet.
So inside main create a new array of parts, string parts.
And it's going to store two parts, Tirhas and Keese.
And the reason we have errors is because of the constructor is now expecting five parameters.
And we're only passing in four values.
So make sure to pass in the part array into each constructor call.
OK, we're good.
And before we run the code removed, the individual calls to drive the drive action is going to play
its role later.
Right now, we don't need it.
Can't run the code.
And these are not spare parts, is it?
Not anymore, but instead of printing a reference to the array we're going to use to string to print
the contents of the array arrays.
To string.
Nissan parts.
Perfect.
We're making a lot of progress.
You said Nissan equal to when you object and passed in five values, Java runs the constructor with
five parameters.
The parts parameter copies, the value inside the parts argument, the reference.
So the parts parameter and parts arguments share a reference to the same array.
Every field updates with a new value.
But notably, we set the parts field equal to a copy of the array.
Now, I need to tell you to pay attention to the reference trap, the reference trap occurs when you
mishandle arrays or objects.
What would happen if we directly set the parts field equal to the parts?
Brammer.
Let's try this out in our code test that parts equal parts.
This is dangerous.
Now, the argument.
The perimeter and field alshehri reference to the same array.
By mistake, let's assume I changed index one from the outside variable parts.
I'll change the next one and set it equal to a written.
Do you see what I mean?
I meant only update the outside variable, the argument we Pastan, but the Nissan field changed as
well.
Have a look, you said Nissan equal to a new object and passed in five values, Gever runs the constructor
with five parameters.
The parts parameter copies, the value inside the parts argument, a reference.
So the parts parameter and parts arguments, a reference to the same array, we said every field equal
to a parameter.
The party field copies the value inside the parts parameter, also a reference.
Now the argument parameter and field share a reference to the same array.
The parameter only exists inside the constructor.
So eventually it falls out of scope.
But the field and outside variables still point to the same array.
And that's why modifying the outside variable directly affected the state of my field.
With that being said, I need to warn you that the reference drop is a recipe for bugs, when many variables
reference the same thing, your application's behavior becomes unpredictable.
Your variables are going to start changing when you don't expect them to.
And you're always going to scratch your head saying what's going on?
To avoid this headache, it's really simple, it should be really careful about how you handle arrays
and objects.
I encourage you to follow my tables and pay attention to the reference Trappe.
So once again, I'm going to set the field equal to a copy of the parameter.
This time around, changing the outside variable should have no effect on the field, a straight out.
And there you go.
All right, we have to make the field private again, every field needs to be private.
The array field is no exception.
And now we have areas in Maine you cannot access a private field, and that's good, you should never
have direct access to a field.
So for now, all we can do is delete the print statements and we're going to add get her in the next
lesson.
In this lesson, you fixed your constructor, the constructor is missing a parameter, so you added
a parts parameter.
Now your constructor is all good because as soon as you create a new object, your constructor updates
every field inside of it.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.