We'll back.
And this video, I would like to show you the functionality and called late in it and then also how
setters and getters work.
So let's create a variable called owner, and it's going to be a type string.
Now, if you create such a variable, as you can see, you always need to initialize it.
And an alternative nearby is to use light in it.
What you say with this is that you will initialize it later on.
So you don't want to initialize it in here, but you will initialize it later.
For example, in the init function.
But there is something that you have to be very careful about when you do something like that, because
if I now create an object of type car, so here var my car is going to be of type car and now I want
to access the owner.
I'll get an error to be more precise, you can see I'm getting a initialise property access exception,
so that's because of this in a property owner has not been initialized.
And that is, of course, because we use late in it and we're trying to access something that is empty,
but it's not a nullable, so we're not catching it.
So what we will need to do is to use the in it so the initialization of our car and in here we can set
the owner to, for example, Frank and out to be more precise.
You can even say this, that owner, because we're talking about this property here.
So if we run this again, then we will see there is no error.
So it doesn't display anything because we don't have any print statements.
But as you can see, it doesn't create an error anymore.
So this is fine.
This works only be careful that you don't use the variable before you have initialized it when you make
it late in it.
All right, the next thing I would like to show you is let's say we have a brand for a car, so it's
going to be a Volvo, my brand, and it's going to be of type string.
And let's say I assign BMW to it.
OK, now I'm going to create a custom getter.
So I'm going to use get.
And then I created a GETER here, which will just return field dot to lower case.
So what that will do is whenever somebody is trying to access my brand, so this variable here, what
they will get is the lower case version of BMW.
So let's have a look at this.
Let's actually use it here.
So here, not the owner, but I want to have my brand and let's in this case print it, print line brand
is and then my car, that brand now could see brand is BMW and BMW is written in a lowercase.
Now, what did we do here?
Well, what we created is a custom, Geter, by default.
Let's say we have a variable called max speed.
So this time is not a vowel.
It's a ba ba max speed is going to be of type int and I'm going to say it's going to be 250 kilometres
per hour.
And by default, what happens is that codon creates the following code for us.
So it says value field equals value.
OK, so this is the default that is generated in the background.
So when we create this variable here, this code here is automatically generated.
We don't need to write it ourselves so we can just get rid of it, because as you saw when we wanted
to use something or as we can see here.
So let me print a line, Max.
Speed is and then we use my car Max Speed, as you can see, we can access it.
So we are getting the value.
We could even go ahead and set the value to 200.
But we would need to do that before, so let me do that real quick, my car thought like speed is going
to be 200, so now we set it.
This is where we set the value.
And in this line, we get to value.
So we are retrieving it so that we are make speed is 200, even though it was 250 here by default.
So bringing this back, this get and set, what you can do is you can customize it.
And we did that with this Geter here for our BMW.
So we did it here.
We create this custom, Geter.
And we can do the same thing with the senators, so if anyone tries to set our maximum speed, we can
check if the maximum speed is at least positive.
So we could even run an error here.
So we can say field, if value is greater zero, then use value.
Otherwise throw an illegal argument exception.
And we're just going to say maximum speed cannot be less than zero.
All right.
So what we have here is an if an else statement just in one line, but of course, you could do that
in multiple lines.
But basically what we're saying here is that if someone tries to set the maximum speed and they tried
to set it to a lower value than zero, then throw an illegal argument exception, which is just going
to say, Max, speed cannot be less than zero.
OK, as you can see here, this get field, this is not a custom field.
So just as redundant, Geter, because we're just using the default ketter here.
So, of course, we could get rid of this whole line here.
All right.
So let's try it.
Let's set the max speed to minus five just to get this error.
So I'm running it.
And as you can see, I'm getting an illegal argument.
Exception, Max.
Speed cannot be less than zero.
So it's basically the text that I defined here for our illegal argument exception.
What happens if we set the positive value here, let's say two hundred forty, then we see the max speed
is 240.
All right, so this is what you need to know about setters and getters.
There's one more thing that I would like to show you, though, and that is, for example, if you have
a model.
So I'm going to create another variable here for my model.
It's going to be of type string and I'm going to assign five to it.
OK.
So, of course, this would be redundant.
We don't need that.
But I'm just going to keep it there.
So what I can do here is I can make the setar private.
So we have seen this private keyword before, but we have not used it.
What does Private do?
Well, Private says that something is only available within the same class.
OK, so in this case, it will only be available within the car.
So if I try to access my model and try to set it somewhere else, it's not going to work.
So let's just try it.
Let's go up there in our main function and go to my car and then try to set my model to something like
M three.
So as you can see, we're getting an error here.
Cannot assign to my model.
The setter is private in car.
To get her, however, is not private.
So if we want to print the car model, print and model is and then we use my car model and here you
can see I don't get an error here because the getter is not private, only the setar, it's private.
So what I did here is I said, OK, you can only change the value of my model within the same class.
So for example, in our in it.
So here I could say this.
My model is going to be an M three instead.
So this will work.
And if I run this, I will be able to get my model and it will now be a three instead of one.
Four and five.
So there we are, models and free.
All right, one little thing that you might have questions around is what this field and this value
are.
Well, Field is a backing field which helps you to refer to properties inside the getter and setter
methods.
This is required because if you use the property directly inside, together or setar, then you run
into a recursive call, which will generate a stack overflow.
So this is just a syntax that you need to use when it comes to fields and value.
Value is the value that is given to the variable when you set it.
So here for Max Speed, for example, this 240 is the value.
So whatever it is after the equals sign is going to be the value that is set.
So you can do something like double check what the value is that is given to you and then you can adjust
accordingly.
And then whatever is the result of this will be then set back to the field, which means just back to
the variable itself.
All right.
So that's setters and getters and also late in it.
And in the next video, we're going to see a little more about object oriented programming.
So see you there.