All right everybody, today I'm going to be explaining getter and setter methods using Java. Getter and setter methods help protect object data, and they add rules for accessing or modifying that data. Here's a demonstration. We're going to create a class of car.
File, New, Class. We will create a car class. A few attributes that cars could have include a string of model, a string of color, and an int, let's say price. Then we'll need a constructor.
We'll have three parameters. A string of model, a string of color, and an int of price. We will assign this.model equals model, this.color equals color, this.price equals price.
Let's construct a car object. Car car equals new car. Pick a model, a year, and a price. I've already used red mustangs for too many examples. I'll pick something else.
Let's say a charger that is yellow, and the price will be ten thousand dollars. I don't know if that's good or not. We don't even know the year of the car.
Anyways, so we have our car object, and then let's output these attributes just to test them. Car dot color. I'll add a space character too.
plus car.model, plus a space character, plus car.price. I'm intentionally not adding a unit of currency. So here's our car. I have a yellow charger, and the price is ten thousand.
Ten thousand of a unit of currency that you choose. But that's not important. With our car object, its attributes are publicly accessible, meaning we can view and change them easily.
To demonstrate, let's say that the model of our car is now a Corvette. We have a yellow Corvette that is $10,000. We don't want the model of our car to magically become a Corvette. Once we assign its model to be Charger, we don't want it to change. What we could do when declaring these attributes is add this axis modifier of private preceding the data type private string model.
Private string color, private int price. We can't access them. And let's test that. You can see that the font color already changed to red. I will attempt to print the car's color, model, and price.
Color has private access in car, model has private access in car, and price has private access in car. Since these attributes are private, We can't normally access them outside of the Car class, but there is a way around that, and that is by using Getter and Setter methods. Getter methods make a field readable.
Setter methods make a field writable. Use Get to Read, Set to Write. We'll set up some Getter methods first.
I would like the car's color, model, and price. Here's how we can do that. Within the Car class, we will create a method.
We'll start with the model. The return type will be string, because our model is a string data type. We'll create a getter method following this naming convention.
Get, then the name of the attribute, model. All we're going to do is return this dot model. And then we'll do this with the color and the price. And I'll just copy this method because I'm feeling a little lazy.
We have get model, get color. return this.color, and get price. Return this.price.
Oh, and this returns an integer, not a string, because our price is an integer. Let's go back to our main Java file. Rather than accessing these attributes directly, because we can't, we're going to call these getter methods getModel if you need the model, getColor if you need the color, getPrice if you need the price. Replace car.color with car.getColor. And this is a method.
car.getModel and car.getPrice. And that should work. Yep, we can now read those attributes. We have a yellow charger, and the price is $10,000. Another thing you can do too with getter methods, you can add additional logic.
So with our getPrice method, let's instead return a string. We will return a unit of currency, I'll pick American dollars, plus the price. Let's see how that changes.
Again, we are calling the get price method. There we go. Yellow charger, $10,000.
Getter methods make a field readable, and you can add additional logic when retrieving one of these attributes. And in this demonstration, I just added a dollar sign just to keep it simple. Then we have Setter Methods.
Setter Methods make a field writable. Going to our Car class, we have Model, Color, and Price. I don't want the Model attribute to be writable.
Once we've declared the model of our car, we don't want to change it. Our Charger can't magically become a Corvette, even though that'd be pretty cool. But the color you can change, you can paint your car, and the price.
You can sell your car for a different price. We'll declare Setter Methods for our color and price, but not the model. because we don't want this attribute to be writable.
So let's scroll down to the bottom. Following a similar pattern, we're going to create a method. The return type is void. This will be setColor.
We have one parameter, a string of color. We will assign this.color equal to the new color that we receive. We'll do this with our price too.
void set price will have one parameter, an int of price. This dot price equals price, the new price that we receive. Okay, let's test these things. I'll attempt to access our color directly. Car dot color equals, I don't know, blue.
That's the first thing that came to mind. And car.price equals... You know what?
The car's on sale. It's $5,000. Well, I can't change these.
We still have that red text for color and price because the color and price attributes are still private. Instead, we're going to call these setter methods. We will set the color and then pass in that new color rather than assign it directly. Set color, pass in a new color.
Set price, pass in a new price. $5000. Then using our getters, we should be able to print these attributes. Yes, we now have a blue charger, and the price is $5000. We don't have a setter method set up for the model.
That's good though. We don't want the model to change. We only have set color and set price. If I attempt to access a set model method, we shouldn't be able to change it. because we never declared a setModel method.
It doesn't exist. But if you did want to change the model, if you want this attribute to be writable, well then you could add a setter method. void set model will pass in a string for the new model. This.model equals model. And then you could change it.
We have a blue corvette for five thousand dollars, which is really cheap. But you know what? I don't want the model to be writable. Just readable. If you don't want an attribute to be writable, when you declare it you can also add this keyword, a final.
That adds an extra security measure. One last thing we should do. With the price, let's add some additional logic.
So with our price, let's do the following. Let's add a check. If our price is less than zero, Then let's output the following price can't be less than zero. Else, if everything checks out, if our price is zero or above, then we'll assign this dot price equals the price that we receive as an argument. Let's test it.
I'll change the color of my car to be blue, but the price is going to be negative $100. We get that message. Price can't be less than zero. The color changed, but not the price.
It stayed the same, at $10,000. So those are getter and setter methods. They help protect object data, and add rules for accessing or modifying that data. Getter methods make a field readable.
Setter methods make a field writable. Depending on your object's attributes, you may want some to be readable, or writable, or both. It's up to you. And well everybody, those are getter and setter methods using Java.