The copy constructor copies every value from one object to another.
In the previous lesson, you decided to sell a second Nissan, so you made a new object and passed in
the same values.
Now, I have to admit, passing every valley twice is a bit annoying.
In this lesson, you're going to get a copy constructor and call it.
Constructor overload means having more than one constructor.
So far, you created one constructor when you pass in for argument's, Java knows to call the constructor
with four parameters.
Now, a class can have many constructors, as many as you want, each constructor shares the same name
as the class that it's in, but it takes different parameters.
You can have a constructor with four parameters, three parameters, two parameters, one parameter
or even zero.
And the fact that each instructor has the same name, how does Javin know which one I intend to call?
Java looks at the arguments you pass, then it's going to know which constructor you want it to call.
For example, you can create a car object and pass in two arguments to the constructor.
Java's going to know to run the constructor with two parameters.
Why does this matter?
Well, we're about to add a new constructor.
And that constructor is the copy constructor, the copy constructor copies every value from one object
to another.
The copy constructor takes one parameter, the object you want to copy values from the source.
As you're creating a new car object, passing the object, you want to copy values from Jarvis's the
argument and it knows to run the copy constructor, the parameter source stores, a reference that points
to the Nissan object.
Then it copies every value from the source object into the current object.
So inside the car class, I'm going out of constructor.
Public car.
And this constructor, for it to run, it expects to receive another car object, car source.
Because of this object is going to be the source from which we update the fields of our new object.
All right, so we're going to set every field in the current object equal to a value from the source
object.
We're going to set the make of the object we just created equal to the make from the source object that
gets passed in.
We'll do the same thing for price.
Year and color.
And now we can call the copy constructor, certainly Nissan, to equal to a new object in UKAR.
And passing the original Nissan is the source object that we want to copy values from.
Reinier code.
And it works every value from the source object gets copied over into the new object.
You set Nisan to equal to a new object and passed the Nissan as an argument, Javert sees the argument
and knows to run the copy constructor.
The parameters source stores a reference that points to the Nissan object, this refers to the new object
we just created, the current object, the one that's calling the constructor and the copy constructor,
copies every field from the source object into the current object.
And in the end, Nissan, too, is equal to a copy of the Nissan object.
This is a way better solution to the reference trap in the old solution, we were using the first constructor
to create copies.
We were passing in four identical arguments.
So Jabarin in the constructor with four parameters.
This method is a bit annoying, in my opinion.
I like the copy constructor because it copies every value from a source.
This is actually the accepted way of copying objects, so we're going to update our table.
And this lesson, you added a copy constructor and used it, the copy constructor takes one parameter,
the object that you want to copy.
As you're creating a new car object, passing the object that you want to copy values from Java, the
argument, and it knows to run the copy constructor.
The parameter source is a reference that points to the Nissan object, and this refers to the new object
you just created, the current object, the one that's calling the constructor and the copy constructor,
copies every field value from the source object into the current object.
This is the best way and the accepted way to copy objects.