Welcome back.
In this video we're going to look at member variables.
And we're going to look at the constructor a little more okay.
So I am in the car dot CSS file.
And here I'm going to create a member variable.
So what is a member variable.
It's a field inside of a class.
So it's a variable that is inside of a class but outside of methods.
So let's say I have this string which I'm going to call model okay.
So I want to know the model of a given car.
Now this model if you hover over it it says field.
So if you're using a field inside of a class that you're creating then it is a member variable of that
class.
So model now is a member variable of the car class.
And it is defining something about that class.
So in this case it's defining the model of that car class.
So generally when it comes to naming there are multiple different ways how you can name member variables
correctly.
A very common name is to use an underscore before the variable name.
And what's also important is that we add the private keyword in front of it.
So this private keyword hides the variable from other classes.
Okay.
We're going to look at that in the next video.
For now I just wanted to have something that allows us to modify any given object of our class car so
that we can distinguish them from each other, because right now, there is no difference between an
Audi or a BMW object here, so we're not saying how they should be different.
However, I would like them to be different, so I would like the Audi to know which model it is.
I would would like the BMW to know which model it is, and that's what this variable should hold.
So how can I now use this variable here?
Well the thing is I can use it inside of my constructor.
And what did I say about the constructor?
The constructor is called whenever I'm creating an object of my given class, in this case of my car
class.
So what I'm going to do is I'm going to say the model should be defined.
Whenever a new object of my car class is created, I can pass data in here into the constructor when
I'm creating a new object of car.
So here I can say something like string model and that model will be whatever the user enters.
And I'm going to store it in this private member variable here okay.
So this here is the parameter that I'm using for my car constructor, which means that when I'm creating
a car object I need to pass data in here.
So here I can pass data.
I can for example say it will be an Audi A3 for example.
And for the BMW I can say it's an i7.
Okay.
So now I'm going to use this information to write something different onto the console.
Here I'm going to say something like a car of the model.
And then the model name has been created.
Plus underscore model has been created.
Okay.
So let me actually put that into two lines.
So it's a little more readable.
So now what I'm saying is when I'm creating a car you need to give me information about the model of
the car.
And when you give me the model of the car, which comes from an external cause, I'm storing that in
my private member variable that holds information about that object.
And then in our case, I'm printing it onto the console.
So let me run that and we will see that we are creating two cars now.
One says a car of the model A3 has been created, and the other one says a car of the model i7 has been
created.
Now, we could obviously also add the brand.
So that's something that I would like you to try and do.
How could you potentially, with two parameters, modify the car class and with another member variable
so that you not only get the model, but also the brand of the car?
Okay, I hope you pause the video now and tried it.
If you didn't, please do so now.
So what we can do is instead of only getting the model, we can also get the brand.
So string brand.
Now I'm going to create another private member variable.
So this one will be underscore brand which I'm going to set to an empty string at the start.
And then I'm going to use the brand here as well.
So underscore brand is going to be brand.
And then I'm going to say a.
Then here we can actually use string manipulation.
Okay a more branch of the model model has been created.
So here we can also use string manipulation where we're just going to say model.
And by the way it doesn't really matter if you use brand or underscore brand.
It isn't in this case really playing a big role.
But why not just use the member variable that we've just created?
So we're saying a brand of the model has been created.
So let's run this again.
And actually we need to go over here and need to modify how we are initializing our car, because now
it's complaining because it is expecting a string model and a string brand.
So the model we have and now I need to add the brand.
So this one will be an Audi.
And this one here will be a BMW.
So now let's run this again and we should see an Audi.
Well we should add the N.
Maybe a Audi of the model A3 has been created and a BMW of the model i7 has been created.
So what we now did is we have now created our own custom constructor.
Now, how is this custom constructor different to any other given constructors?
Well, that's something that we're going to look at later on.
But for now what you need to memorize is that we have this concept of member variables where when we
are naming them, we add the underscore and we are making them private, which hides the variable from
other classes.
So that means that I cannot access BMW for example BMW dot underscore.
And then I cannot access this model for example.
So I cannot say dot underscore model I cannot access it.
But we are going to see how we can change that.
And then we saw how we can modify our constructor to take one parameter or even two parameters and work
with them.
So that's it for this video.
In the next video, we're going to look into how we can modify the hiding.
So how we can unhide a member variable for example.