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.