Objects make your code organized and easier to understand.
Object oriented programming means organizing your code around objects.
And the purpose of object oriented programming is to write high quality code.
In this lesson, you will create two car objects.
Before we start, create a new folder.
Call it section seven.
Inside the folder, create a file named Main Java, and then make sure that your class has a main method.
So your boss wants you to write code based on these requirements.
And before you write a single line, step one is to identify the objects.
We can define what an object is based on two criterias.
An object is a thing that contains fields.
And furthermore, an object is a thing that performs tasks.
Take some time to remember these two bullet points.
Now then, how many objects can you identify from this passage?
I only count one type of object car.
So based on the first bullet point, which describes an object as a thing that contains fields, car
is a thing that contains the fields make price year end color.
Going back to our criteria, an object is also a thing that can perform tasks.
In this case, a car object can drive.
So let's keep track of our progress.
We identified one object, a car.
A car is defined by its make price year end color.
And furthermore, a car can drive.
So that was step one.
You identified one type of object car.
Step two is to create a class.
The class is a blueprint from which you can create an object.
Previously we identified that every car is defined by its make price year and color.
So based on these fields, we can create a blueprint.
Our class, our blueprint, will declare that every single car needs to define a make price year end
color.
Inside your project.
Create a new file called Car Java.
And inside the car class, we declare that every single car needs to define a make.
It needs to define a price.
A year.
And a color.
And that's all.
The car class is a blueprint, and from this blueprint we can create many car objects.
And that's actually step number three from the car class.
You will create car objects.
The object will be of type car.
Here we are creating a new object of the class.
The variable automatically stores a reference to that object.
And once I create an object, I can modify its fields.
So what we'll do when our code is from the car class, from the blueprint that we just developed, we're
going to create two car objects.
The first object will be if type car.
I will call the variable Nissan, and the Nissan variable will store a reference to a brand new object
of the car class.
This car object is automatically going to contain four fields.
We can set the first field the make equal to Nissan.
We can set its price equal to $10,000.
We can set it's year.
Equal to 2020.
And we can set its color.
Be equal to green.
Let's visualize the runtime.
Here we are creating a new object of the car class and the variable Nissan stores a reference to that
object.
And the object is fields by default are going to start at zero or null.
Here we update that object as make to Nissan.
Here we update it's priced at $10,000.
Here we update that object says year to 2020.
And here we update this object as color to green.
And how easy was that?
Using the same blueprint, I can create another object of the car class.
I will call this variable dodge.
And the Dodge variable will store a reference to a brand new object of the car class.
This brand new car object is going to have a make that equals dodge.
It's going to have a price that equals 11,000.
It's going to have a year.
Of 2019.
And it will have a color of blue.
Before we visualize the runtime, there is a text file inside of your resources folder.
Please take some time to download it.
The file that you just downloaded contains code that prints the fields of each object.
So copy it into your main class and let us once again visualize the runtime.
Press debug.
Here we are creating a new object of the class.
This variable, Nissan automatically stores a reference to that object.
The object is fields by default start zero or null.
Here we update that object as make.
Here we update its price.
Here we update its year and here we update its color.
Here we're creating another object of the car class, and the variable dodge is going to store a reference
that points to that object.
The object is fields by default, start at zero or null, and once again, we update every single field
in that object.
At this point, we successfully created two objects of the car class, and finally, we can print the
fields of each object.
The first object, if I hover over the variable that points to it you can see, has a make of Nissan
a price of 10,000 a year of 2020 and a color of green.
So that's what's going to print.
Beautiful.
The second object, if I hover on the variable that points to it, has a make of dodge, a color of
blue, a price of 11,000 and a year of 2019.
Let's just press continue.
And that is exactly what Prince.
So to recap, you created two objects of the car class.
A car class is a blueprint from which you can create concrete car objects.