In this lesson, you will add setters to the car class.
Currently we have no way of updating an object as field because they were made to be private to the
class that they were defined in.
So that's why we need to add some public setters.
So here we say public.
Void.
And a setter is always going to be called set followed by the field name.
A Sutter does expect to receive a parameter whose type depends on the field itself.
So this is going to receive a make value.
And it's going to set the field.
And the current object that's calling this setter, this dot make equal to the make value that gets
passed in.
So notice here we do need the this keyword to distinguish between the field and the parameter that gets
passed in.
Whereas in the getter there are no parameters.
So make will automatically refer to the object as field.
All right.
So now we will add another setter that is available to the public.
This one will be called set price.
It's going to receive a double price, the same type as your price field.
And when this setter gets called, it's going to set the price field of the current object equal to
the price value that gets passed in.
All right.
Now we're going to need another setter, public void.
This one will be called set year.
And it's going to receive a year value.
We can reference the current object that calls this method using the this keyword, and we can set its
year field equal to the year value that gets passed in.
Finally, we need one more setter.
Here we'll say public void.
This setter will be called set color and its responsibility will be to update the color field with whatever
color value gets passed in.
And the current object that calls this setter.
We're going to set its color field equal to whatever color value gets passed in.
And that should be all.
And now, even though our fields are private, we have public gutters to return the fields as value
and we have public setters to update them.
So here what I'll do before printing the objects is Fields will update the color of this object.
So here we'll say Nissan set color, and we're going to update its color to jet black.
And we'll do the same thing for this object.
We will say Dodge dot, set color, and we're going to give it a color of jet black as well.
All right.
And you know what?
Let's just say the dealership is having a 50% off sale.
What we'll do is we'll say Nissan DOT set price and we're going to set its price field equal to the
current price.
Divided by two.
You can do the same thing here.
Dodge Dot Set price.
That's going to equal the current price of the Dodge Object divided by two.
All right.
I'm going to need break points in set color, set price and get price.
I'll put four breakpoints here and we're good to go.
So at this point we have two car objects.
The first thing we'll do is from this object, we're going to call set color.
So here this points to the current object that called this method.
And here we're setting this color equal to the color value that was passed in jet black.
All right.
Now we're calling set color from this object, the object with a reference of 11.
We can step inside.
This points to the current object called this method.
And here we're setting this color.
Equal to the color value that was passed in jet black.
All right.
So at this point, we've updated both of our objects as color fields.
And now here, Nissan is going to update its price field by first getting its current price.
So get price is going to return the price field of the current object.
So 10,000 divided by two means we're passing 5000 into set price.
This points to the current object that's calling set price, and we're setting this price equal to 5000.
All right.
Going to the next breakpoint.
Here.
We're setting the price for this object by getting its current price divided by two.
So first, we're going to go to get price.
Get price is going to return the price value of the current object that's calling it 11,000.
The value returned is 11,000.
That divided by two is 5500.
That's what's going to get passed into set price when an object with a reference of 11 calls it.
This points to the current object that called set price.
And here we're setting this price.
The current object is price field equal to 5500.
So at this point, we have fully updated both objects, the object with a reference of ten.
The object with a reference of 11.
And here we're just printing all of the fields.
I don't want to step inside of get price again, so let me just remove all of my breakpoints.
And we are done.
So this recap is fairly simple.
You should use setters to update your objects as fields.