All language subtitles for 018 Step 2 - Getters_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

The next step is to try to get her.

Every field needs a getter, the parts field is private, yet it doesn't have one.

This means we have no way of accessing it from outside.

So in this lesson, we're going to be together for the spare parts field.

The getter is going to be public.

It's going to return a string array.

It's going to be lower, Campbell case, it'll start with the word get an end with the field name and

that's all.

Now, you might be tempted to write return this parts.

Let's return it for now, return this stock parts.

And now called the Geter.

Nissan aget parts.

And set a variable equal to the return value, just some random variable.

OK, now I'm going to change index one of the outside variable to hello.

And then we're going to access the field from the Nissan once again, and I'm going to print every element

inside of it as one long string.

All right, this is bad, I change the outside variable, but the field from the Nissan object got affected

as well.

If you return the field, what happened is that Jova returns a copy of what's inside and what's inside

is a reference.

And so the getter is supposed to protect your field, but it's returning a reference that points directly

to it.

This actually defeats the purpose of a geter, now the outside variable and field share a reference

to the same array.

If I update the outside variable, the field gets affected as well.

This is something I like to call reference, trapped three year geter should not return an array field

directly.

The pitfall is that it will return a reference that points directly to the field.

So we need to make sure that they get a returns, a copy of the array.

This table is going to be in your cheat sheet.

Don't worry.

So back to our Geter, we're going to use a raised copy of to create a new copy of our field.

And we're going to copy the entire length of the array.

All right, now run the code.

And splendid, changing the outside variable has no effect on the field.

And that's because the getter returns a new copy of the array field, and so now you're done adding

the getter and your code is bulletproof.

There is no chance of you accidentally changing any of your fields.

Every field is protected by the private keyword and each getter returns a secure copy of the field.

OK, so now we can remove the random variable we made, it was just for testing purposes.

But there's still one issue try printing the parts field from Nissan to.

And we get a null pointer exception.

That's because the parts field for the Nissan object or for the Nissan to object, I should say, is

null, and attempting to access or call something from a null causes a crush, an exception.

And that's why the crash is happening inside line 41 of the car class.

So you might be asking, why is Nissan TEUs Hartsfield null?

I'll give you a hint.

Think about which constructor the Nissan two object uses to update its fields'.

I hope you got it.

We're going to talk more about this before moving on to step three.

In this lesson, we added a GETER for the parts field getters are supposed to return secure copies of

each field, so you have to be careful about returning a reference that points directly to your field.

If you return the field, Java returns a copy of what's inside and what's inside is a reference.

This defeats the purpose of the GETER.

The Geter are supposed to protect our field, but it returns a reference that points directly to it.

And now the outside variable has direct access to the same array as your field.

And this is really dangerous.

This is reference Tropp three.

Your Geter should not return an array field directly.

The pitfall is it's going to return a reference that points directly to the field.

So we need to make sure that the getter returns a copy of the array.

We did that in our code, the GETER created a copy of the field and returned it.

Ultimately, the getter keeps your field safe by returning a copy of it.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.