Now that we've seen how methods work inside of classes, we're going to take a look at how we handled
data inside them as well to get started.
I'm going to clean up this air right here around vehicle honk by deleting that line, and I'll do the
same thing at the bottom of this file as well.
All right, it's now back at the top on vehicle, I want to add in to fields, these are essentially
going to be properties are going to reference actual values.
Whenever you want to specify a property inside of a class, we'll just write out the name of the property,
so maybe color and then a type invitation for that property.
So in our case, maybe a color should be a string like so.
And right now, as soon as you type out this line of code, you might see an air around this line,
specifically around color.
If you don't see an air in your screen, looks like mine, that's totally fine.
If you do see and there it is, because traditionally we want to initialize a variable any time we declare
it inside of a class.
So right now we have declared the existence of a property or a field attached to class vehicle, but
we have not give it and given it an initial value to do so right after the class or the property definition.
We can write in an equal sign and then some value.
In my case I'll say read like so.
So this means that every time we create an instance of class vehicle, the instance will have a color
property that is a string and its starting value will be equal to red.
So, for example, down here, right underneath CONSED vehicle is new vehicle.
We could do a console log of vehicle, not color like so.
If we now save this, look back over to our terminal one more time and run that file.
We'll see a council log of red up here.
Now, this is just one way to initialize a variable inside of a class.
It's also very common to want to initialize a variable using some configuration options that we passed
to the class when we create an instance out of it.
So, for example, it'd be a lot nicer to specify the color, right, when we create a vehicle, because
sometimes a vehicle might have a color of red.
Other times it might be silver or black or orange or whatever it might be.
So to do so, we could pass in the color as an argument to vehicle right here.
So maybe I'll do something like orange.
If we're going to pass in any arguments to this, we have to define a special function inside of the
class definition called the constructor.
So I'm going to write out constructor like so the constructor function is a very special function defined
inside of a class, any time that we define a constructor, it will be instantly executed, right when
we create a new instance of the class.
So as soon as we call new vehicle right here, the constructor function is going to run and the arguments
that function are going to be whatever we pass in when we create the instance.
So in this case would be a string of orange.
So we would probably want to receive that as an argument into the constructor of something like maybe
color with an annotation of string like so.
And then we'll take that value and do some initial assignment with it inside the constructor, so we
would say this color is equal to color.
If we use this method of initializing color by defining the constructor, we do not have to initialize
it up here.
So it's usually an either or decision.
Either you are going to initialize a property on the same line that you define it or you're going to
initialize it inside the constructor.
One or the other in this case will stick with the constructor method.
So I'm going to delete the equals.
Read up here.
So now if I save this file again, I can run it in my terminal and I should now see I got a little air
here.
Let's see.
Argument for color was not provided by mistake.
Let's see.
Oh, I know what it is.
We created a car down here as well.
Let's just comment out all this stuff down here really quickly.
While we were working on that first class, this actually is a very valuable subject that we have to
discuss, like what we do with constructor's in extended classes, but let's just hold off on that for
right now.
So I just commented out everything in the second half to file.
I'm going to save the file and then try to run it again.
And there we go.
Now I see orange.
Perfect.
Now, just you know, there's also a little shortcut to automate this entire process right here to notice
how we kind of have to refer to these repetitive properties multiple times.
I see color, color, color all over the place.
A equivalent way to write all this out by using a little shortcut would be to delete the implementation
inside the constructor.
Delete the property definition at the top.
And then right in front of the color argument, we could add on the word public like so.
Public is one of those modifiers we spoke about, these modifiers can be used not only on methods in
a class, but on properties or fields as well.
So this syntax you see right here is going to mean that we're going to take whatever comes in as the
first argument to that constructor, which is in this case, it's orange and it will be assigned, as
an instance, variable of color to our vehicle that we are creating.
So the code that you see right here is 100 percent equivalent to this right here.
It's just slightly shortened up.
So we don't have to refer to the same color, color, color all over the place.
So we're going to use this shortcut many times in the code that we write, if we do something like this,
we can then shorten up that body of the constructor function as well.
Even though it's empty, we still have to put it in.
So let's try saving this flip back over, run the file again, and we will see orange again.
All right, now, those modifiers we spoke about apply to variables just the same way as they do to
methods so we could alternatively use the private keyword or the private modifier on color.
If we do so, we will no longer be able to read that variable outside of our class anymore.
Likewise, we could also mark this thing as being protected.
We still cannot access it outside the class, but we would be able to access that field from a class
that extends vehicle such as car down here.
All right, I'm going to leave that variable as public like so and I'll say the file, we're going to
leave it like this.
Let's take a quick pause right here.
We're going to come back the next video and talk about how these fields work when we are using extensions
like we are down here.
So quick pause and I'll see you in just a minute.