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
If a class has failed, you need to add getas.
The cars is private, as it should be.
This means we can't access the field from outside.
So in this lesson, we're going to to get her.
The dealership class has one field, so we need to add one Geter.
So should ever get her be called get cars.
I hope you guessed the answer no, because let's imagine a scenario where I only want to access the
car at index zero, get cars, would create copies of every object in the car's array, but I only need
one of them.
This is a waste of memory.
When you create new copies of objects, it's not free.
It takes up memory.
It takes up space in your computer.
And so what if my array has a thousand cars?
Are you going to copy a thousand car objects, return them for the sake of accessing one?
Once again, this is typical to a class that manages an array of objects.
You just never know how many objects are going to be inside of that array.
And so it doesn't make sense to return every object at once.
Instead, the gutter is going to return one car object from an index of your choice.
So inside your dealership class at the get or get car public, it's going to return a car object.
Its name will be Get Car and it will take one parameter, the index of the car you want to get.
Finally, return the car object found at the index chosen by the user.
And now we can access the cars that we want.
In Maine and get the car object at index zero.
And the GETER is going to return the card index zero.
Pretty simple.
Let's print the new car object and make sure everything works fine.
And nice, when you print an object, Java knows to check if there's a two string method that returns
a string of every field.
Sounds great, right?
Let's update the new cars, clarefield new car at color.
Blue.
And then we're going to access another copy of the car object that in zero and print that object instead.
Well, what happened, this is bad, I change the outside variable, but the object at Index zero,
the cars feel changed as well.
If you return the object, Jaba returns a copy of what's inside the element and what's inside is a reference.
The letter is supposed to protect your field, but it returns a reference that points directly to one
of the objects.
This defeats the purpose of a letter.
Now the outside variable and field share a reference to the same object.
This is the last reference trap, reference trap for your Geter should not return an object directly.
The pitfall is that the outside variable and field are going to share a reference to the same object.
The solution is simple return a new copy of the object.
This table is in your cheat sheet, so we're going to return a new car object.
And in brackets, put the source object you want to copy values from we would like to copy values from
the car object and the index chosen by the user and right after creating a new car object or passing
any source object so Java knows to run the copy constructor, that receives one as a parameter.
And it's going to copy every field value from the source object into the one we just created.
And then the GETER is going to return a new copy, which gives me comfort, because now I know that
there's no way any of the objects inside the cars field are going to leak across the gutter, keeps
our field safe by returning copies of the underlying car objects.
Anyways, run the code.
And sweet, changing the outside variable has no effect on the field.
That's because the getter returns a copy of the object we're trying to access first, it indexes an
object from the car's array, but it returns a copy of it.
Ultimately, the letter keeps your field safe.
Our field is now fully secure and private, there is no chance of it leaking access.
Once again, by following these beautiful tables, we made sure the code is strong and bulletproof.
All right, now we can remove the random variable we made.
And that's all.
In this lesson, we added a geter, the Geter get car returns, one car object at the index of your
choice.
It indexes an object from the car's array and then it returns a new copy of the object.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.