Now that we've seen how methods work inside of classes we're going to take a look at how he handled
data inside them as well to get started.
I'm going to clean up this air right here around vehicle dot honk by deleting that line.
I'll do the same thing at the bottom this file as well all right.
It's now back at the top on vehicle.
I want to add in two 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 annotation for that property.
So in our case maybe color should be a string like so 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 error in your screen looks like mine that's totally fine.
If you do see an error it is because traditionally we want to initialize a variable.
Anytime 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 in 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 red 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 it's starting value will be equal to red.
So for example down here right underneath const vehicle is new vehicle we could do a console log of
vehicle dot color like so if we now save this lot back over to our terminal one more time and run that
file we'll see a console log of red appear.
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 so we passed
to the class when we create an instance out of it.
So for example it would be a lot nicer to specify the color right when we create a vehicle because sometimes
a vehicle might have a color 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 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 it's like as soon as we called 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 dot color is equal to color if we'd 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 we'll 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 get a little air here.
Let's see argument for color was not provided my 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 constructors in
extended classes but let's just hold off on that for right now.
So I just commented out everything in the second I the file and the Save the file and then try to run
it again.
And there we go.
Now I see orange perfect now just so you know there's also a little shortcut to automate this entire
process right here.
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 class but on properties or fields as well.
So the 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 is 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 is 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 let's try saving this flip back over run the file again
and we'll see orange again
all right.
Now there's modifiers we spoke about applied 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 can 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 gonna 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.