In this lesson, you will add getters to the car class.
You might be wondering, everything is working nice and perfect.
Why are we adding getters?
The issue with the syntax is let's just say my intention is to just access the objects as make field.
Well, using this same syntax, I could accidentally update this field to be Toyota when in fact it
should have a string called Nissan.
So having direct access to an object field is not very safe and it's considered very, very bad practice,
which is why we have to make every single field private to the car class.
So private means that the field is private to the class that it was defined in.
It is not visible anywhere else.
So if we go back to Main Java here, we're trying to directly access the make field, but the make field
is not visible.
Each field is private to the class that it was defined in.
It is not visible anywhere else.
The thing is, I still need to print the Nissan object's fields.
So how can I get all of its field values?
So while the fields are private, a getter is a public method.
Whose return type depends on a particular field and it's always going to be called get followed by the
name of your field again, using camel case.
And what it will do is return the field of the current object that calls this method.
Now we'll create another gutter.
This one needs to return a double.
It will be called Get price.
And it will return the price field of the current object that calls this method.
Now notice we don't need to say this make or this the price because there is no conflicting parameter.
Java is smart enough to return the make field of the current object calls this method.
Now we're going to need another getter.
This one will return an integer.
It's going to be called get.
Yea.
And it needs to return the year.
Of whatever object calls this method.
The final gutter we need to declare is one that returns a string.
It will be called Get Color.
And it needs to return the color field of whatever object is currently calling this method.
So now what we have is perfect.
Our fields are safe, sound and private, and each field has a public getter that returns a copy of
its value.
So now we're good to go.
Here because we no longer have direct access to an object as field.
What we can do is call it's getter.
So Nissan get make Nissan get price.
Nissan jet year and Nissan jet color.
We can do the same thing here from the Dodge object.
We can call get make here.
We can call get price get year.
And get color.
Okay, I'm going to add breakpoints inside every getter because it's going to be really hard pressing
the step in to button when we're already inside the print line function.
So we'll put breakpoints here.
So at this point, we've created two new objects of the car class.
Each variable stores a reference to that object.
First we call get make for this object.
This refers to the current object that's calling this method.
And here we return the make field of that object.
The second getter that gets called was get price.
Which is going to return the price field of the current object.
The third getter that gets called is get Yea, which is going to return the year field of the current
object.
And the fourth getter that gets called is get Cutler, which is going to return the color field of the
current object.
If I press next, it prints every single field inside of the Nissan object.
And now once again, we call get make, get price, get yea get color, but we're going to call these
gutters for this object.
And so once again.
This points to the current object that's calling this method.
That object is make is dodge.
That's what's going to get returned.
Here we return the price of the current object.
Here we return the year of the current object.
And here we return the color.
And that's it.
So to recap, you should always use gutters to access your objects as fields.