Welcome back.
In this video we're going to look at the static field slash static variable.
And therefore inside of my card class I'm going to create a public static int number of cars which I'm
going to set to zero.
And what I'm going to do is each time that I'm creating a new car.
So here I'm going to say number of cars plus plus.
So I'm going to increment the amount of cars that I have.
Now if you want to be really precise about it you would need to make sure that the CTR.
So here in this case the default constructor also has an increment in here because well right now we
could go ahead and create a car that is well we don't add any details to it.
But then we want to make sure that the number of cars is incremented then as well.
So we have this static field here.
This is a field right now.
This time it's not a private member variable.
So we don't need to use an underscore here.
And because it is a static variable we are using the capital letter here starting the variable name.
So it's public so that I can access it from outside the car class.
So what this will now do for us is it will now allow us to know how many cars we are producing.
So let's go over to our programs class here.
And I'm going to get rid of our other code that we had just now.
So here I'm creating a car, new car.
Then I'm creating another car car two.
And then I'm creating another car.
Car three is going to be a new car, which will be let's say an A3, and this will be B will be an Audi,
and it will not be a luxury car.
And now what I can do is I can access the static variable car dot number of cars directly.
Okay.
So I cannot access it on an object of car.
I can only access it directly.
So I could just say for example c w number of cars produced and then plus number of cars or cars dot
number of cars like so.
And now let's run our app to see it in action.
And we see a number of cars produced three.
And let's double check car one, car two, car three.
So there we are.
Now you've seen the power of using a static or public static variable.
So accessing the public static variable number of cars of the car class.
Cool.
So that's another very useful case or use case for the static keyword.
So that's it for this video.
See you in the next one.