Hi everyone.
Welcome back to this tutorial.
In this tutorial we will talk about constructor reference.
This concept is introduced as part of Java eight.
From the name, it is clear that this concept is related to constructors in Java.
Constructors in Java gets invoked during the creation of an object.
Let's take a look at the syntax.
The syntax is almost the same as the method reference, but here we have the new keyword after the double
colon instead of a method name in method reference.
Let's take a look at some code examples.
You can only use the constructor reference in the context of functional interfaces.
This example here we have the supplier function interface followed by a name, and then in the right
hand side we have the student class.
Followed by that we have this double colon operator and then the new keyword.
What this does is it creates a supplier which is going to return a brand new object whenever the get
method is invoked in this supplier function interface.
The expectation here is that the student class should have an empty constructor if we try to use a similar
syntax to create objects like here, then you will end up having compilation issues.
Let's go ahead and code this one.
When I have the enclosure open.
There you go.
I have the IntelliJ.
And then the next step is I'm going to create a package called constructor reference.
The two reference.
And then what I'm going to do is I'm going to create a class called.
Constructor.
Reference example, I want to make this class executable.
There you go.
It's ready.
The next step is let's create a supplier.
What are we saw in the example and then pass the student as a type to the supplier.
Let's import the player.
There we go.
It's ready.
And then we call student.
We call the name as student supplier for that one.
And then the next thing is we're going to do Student Colon.
Colon New.
Basically this is going to create a student object.
Let's see what it is complaining about.
So as I mentioned in the presentation, we need to have an empty constructor right now.
The student class doesn't have it.
That's the reason why it is complaining about it.
So we have created the student constructor.
Right now we have it.
Now if we go and take a look at it, there is no compilation issue.
It's good.
Let's call this supplier.
He runs a player.
Needs to be.
Prepended with static keyword because we are trying to use that in the static method.
The student supplier dot get.
If you run this, it is going to print an empty object.
Meaning object, which has all the attributes populated as null.
Now.
Let's create one more example of this one.
I'm going to create a function.
Is going to accept a string.
And then the output is going to be student.
So the first parameter here is the input and the second one is the output.
What I'm going to do, I'm going to.
All the same thing.
Shouldn't colon colon new?
Now it will complain because it is expecting one argument or one parameter constructor.
But if you take a look at the student class, we just have these two constructors.
One is an empty constructor and the other one is a constructor which has all the properties.
Now we will have to create the constructor, which takes one argument.
Which is of type string.
And then we are going to set this one as name, this dot name equal to this.
Now if I call this function right?
All this didn't.
Function dot apply.
Because apply is the method which we use in order to pass an input to the function functional interface.
Then I'm going to pass the name as ABC.
If I run, this is going to print the same object, meaning similar object, but name will have ABC
instead of null.
There you go.
It printed the student object, but with the name as ABC.
This is all about constructor reference.
With this we came to the end of this tutorial.