- [Instructor] Static methods are methods
that aren't accessible through an instance of a class,
but only available through the class itself.
They are usually created for utility functions
that don't relate to the instance of the class.
I rarely need to use static methods,
but when I do, it's always good to be aware they exist.
Let me demonstrate.
So, in our code here, we'll create
a static methods inside of our class here.
So go ahead and go to line 13 and return
and let's create the method static total doors.
So, this method will take two cars and calculate
the amount of doors that we have in between the two cars.
So let's go ahead and pass the arguments, car one, car two.
And then we'll create a variable called const doors one
and pass car doors to this variable.
And then let's do the exact same thing here.
So let's copy line 16, let's paste it to line 17,
and let's simply option click and change it
to doors two, cars two, and then what we'll do
is return doors one plus doors two.
So right now, let's save this
and let's look inside of CX5 on our Sandbox.
So let's go here and then take a look at the car.
And we don't have that function available here.
If we look at the prototype, we have car stats,
but we don't have the static method that we just created,
total doors, it's actually part of the class car here,
so if we click on that constructor, we'll see total doors,
but we can't access it from the actual CX5.
So if we try this with CX5 here,
we won't be able to access it, it'll throw us an error.
To use it, we need to use the car class.
So in this case, we'd do something like car.totaldoors
and then pass the cars to it.
So we'll need to create a second car for this to work,
so let's go and create a second car.
So this one, let's do something like a Civic
and do a new car and for this one,
again, I don't know a lot about cars,
so I could be completely off here,
I'm just simply passing values to it.
So V4 and then pass the color blue.
Alright, so I got three doors, a V4 engine,
and then color blue, perfect.
So I got everything that I need here,
and then we can use the total car function
and pass it to car, so I can pass this CX5
and I can pass the Civic.
Now it's gonna work, so let's go back to our Sandbox.
And now I have this second car here, the number seven.
So I have seven doors in between the two cars,
and this is working, so let's go and call also the Civic.
So let's go in, call these two.
So copy line 26, 27, paste it, and then,
instead of the CX5, I'm gonna do option click
to select the second one, do a Civic, save,
and let's go back here.
I got the first car that has four doors, V6, gray.
Second car, three doors, engine V4, and blue.
And then the total amount of doors
in between those two is seven.
So this is how you use static methods.
These are utility functions that you can create
for the class, but not passable to the actual instance
of that class and not usable for those,
but simply tools that you'd like to use
for other means outside of the instance of that class.