Hi everyone!
In this tutorial, we are going to look at Java constructor chaining.
If you remember in the last video we ran into problems initializing our class.
We were setting the same starting yardline in every constructor in our football game.
Our concern was if the rules to football changed again, we would need to check every Java constructor
to make sure we were setting the value correctly.
We could set the value as a constant, which would reduce the number of places we need
to refactor.
That would be a good solution.
Let's assume though we also have the requirement to play according to different football rules
for different years.
We need to calculate the correct starting position based on the year.
That means a constant no longer works for us.
We'll start by creating a method that provides the starting yardline for a given year.
This will return the value we need to initialize our game class.
So now we can set the value, but we're still not solving our problem.
We are actually in a worse position.
We could forget to update a constructor, and now in one place we're using a constant and
the other we're using the method.
What we really want to do is set the value in one place, only once.
What we need to do is call one constructor from another constructor.
This is called constructor chaining.
We're chaining our constructors together, and calling each one in order before returning
a created instance.
We do that by using the this keyword as a method.
The this keyword looks for another constructor matching the argument list we provide.
There are a few gotchas though.
If the parameter list does not match a constructor, our code will not compile.
And of course we cannot have multiple constructors with the same parameter list, even if the
parameter names are different.
Java wouldn't let us do that even if we didn't chain constructors.
The this keyword also only works with constructors for the current class.
We cannot use it to call constructors in our super class.
To do that, we'd use the super keyword to call the parent class.
When we do use it, it must be the first line in the constructor.
We also cannot do this more than once, meaning you cannot call more constructors once you’ve
called another constructor.
You only get to call one.
And the big gotcha is we cannot chain constructors in a way to create a loop.
Java won't even let us compile constructors with a loop.
We'll get an exception.
When we create our constructor chain, we need to keep the chain in a straight line as much
as possible.
An inverted tree would work too, but that can get complicated for other developers to
figure out if our tree gets too big.
Ultimately our constructor chain should end up at a single constructor.
That constructor should be the only constructor responsible for returning an instance.
We can create multiple independent chains, but that gets confusing too.
Best practice is one chain, with one ending.
A classic pattern for chaining constructors is to perform one initialization step each
time.
The starting constructor sets everything to a default value.
Then each constructor adds one bit of information based on it's parameter.
If we have a situation where the construction path is simple, or we would always have initialization
values known in a particular order, chaining constructors works well.
Of course, there's a slight problem with this.
What if we have only three of five parameters?
There's no real good way to jump in the middle of the chain, or skip constructors.
We'll need another approach.
In the next tutorial we'll look at something called the builder pattern to solve our initialization
order problem.
Thanks for watching!
If you have any questions let me know in the comments.
New videos come out every week, so make sure you subscribe.
You don’t want to miss a video!
If you're trying to get to the deegeu.com website, it's down for the next week for a
site update.
Videos will still be posted.
And with that, I’ll see you in the next tutorial!