WEBVTT

00:00.230 --> 00:00.740
Hi everyone.

00:00.740 --> 00:02.060
Welcome back to this tutorial.

00:02.060 --> 00:05.750
In this tutorial we will talk about constructor reference.

00:06.740 --> 00:10.070
This concept is introduced as part of Java eight.

00:10.100 --> 00:15.860
From the name, it is clear that this concept is related to constructors in Java.

00:16.010 --> 00:20.360
Constructors in Java gets invoked during the creation of an object.

00:21.260 --> 00:22.730
Let's take a look at the syntax.

00:22.820 --> 00:29.780
The syntax is almost the same as the method reference, but here we have the new keyword after the double

00:29.780 --> 00:33.680
colon instead of a method name in method reference.

00:34.550 --> 00:36.890
Let's take a look at some code examples.

00:37.960 --> 00:42.460
You can only use the constructor reference in the context of functional interfaces.

00:42.490 --> 00:47.830
This example here we have the supplier function interface followed by a name, and then in the right

00:47.830 --> 00:50.350
hand side we have the student class.

00:50.500 --> 00:55.400
Followed by that we have this double colon operator and then the new keyword.

00:55.420 --> 01:02.770
What this does is it creates a supplier which is going to return a brand new object whenever the get

01:02.800 --> 01:06.040
method is invoked in this supplier function interface.

01:08.130 --> 01:13.980
The expectation here is that the student class should have an empty constructor if we try to use a similar

01:13.980 --> 01:19.990
syntax to create objects like here, then you will end up having compilation issues.

01:20.010 --> 01:21.630
Let's go ahead and code this one.

01:22.500 --> 01:23.970
When I have the enclosure open.

01:24.000 --> 01:24.480
There you go.

01:24.480 --> 01:25.380
I have the IntelliJ.

01:25.680 --> 01:30.750
And then the next step is I'm going to create a package called constructor reference.

01:33.050 --> 01:34.130
The two reference.

01:36.040 --> 01:40.090
And then what I'm going to do is I'm going to create a class called.

01:41.120 --> 01:42.110
Constructor.

01:43.620 --> 01:47.820
Reference example, I want to make this class executable.

01:49.380 --> 01:49.990
There you go.

01:50.010 --> 01:50.700
It's ready.

01:50.820 --> 01:53.760
The next step is let's create a supplier.

01:53.970 --> 02:01.470
What are we saw in the example and then pass the student as a type to the supplier.

02:02.340 --> 02:03.900
Let's import the player.

02:04.500 --> 02:05.180
There we go.

02:05.190 --> 02:05.910
It's ready.

02:05.910 --> 02:07.730
And then we call student.

02:07.740 --> 02:10.530
We call the name as student supplier for that one.

02:10.800 --> 02:16.290
And then the next thing is we're going to do Student Colon.

02:16.290 --> 02:17.370
Colon New.

02:18.660 --> 02:22.230
Basically this is going to create a student object.

02:22.260 --> 02:24.210
Let's see what it is complaining about.

02:24.780 --> 02:30.090
So as I mentioned in the presentation, we need to have an empty constructor right now.

02:30.090 --> 02:31.620
The student class doesn't have it.

02:31.620 --> 02:33.840
That's the reason why it is complaining about it.

02:34.570 --> 02:37.270
So we have created the student constructor.

02:37.270 --> 02:39.110
Right now we have it.

02:39.130 --> 02:43.300
Now if we go and take a look at it, there is no compilation issue.

02:43.450 --> 02:44.080
It's good.

02:44.410 --> 02:47.020
Let's call this supplier.

02:48.470 --> 02:49.490
He runs a player.

02:51.840 --> 02:52.890
Needs to be.

02:54.430 --> 02:59.350
Prepended with static keyword because we are trying to use that in the static method.

02:59.710 --> 03:02.020
The student supplier dot get.

03:02.860 --> 03:05.800
If you run this, it is going to print an empty object.

03:06.790 --> 03:11.590
Meaning object, which has all the attributes populated as null.

03:11.620 --> 03:12.550
Now.

03:13.350 --> 03:15.300
Let's create one more example of this one.

03:15.300 --> 03:17.670
I'm going to create a function.

03:20.090 --> 03:22.220
Is going to accept a string.

03:23.550 --> 03:27.150
And then the output is going to be student.

03:27.150 --> 03:32.400
So the first parameter here is the input and the second one is the output.

03:32.790 --> 03:35.400
What I'm going to do, I'm going to.

03:37.120 --> 03:37.870
All the same thing.

03:37.950 --> 03:39.430
Shouldn't colon colon new?

03:40.620 --> 03:47.520
Now it will complain because it is expecting one argument or one parameter constructor.

03:47.520 --> 03:51.210
But if you take a look at the student class, we just have these two constructors.

03:51.210 --> 03:55.950
One is an empty constructor and the other one is a constructor which has all the properties.

03:55.980 --> 04:00.990
Now we will have to create the constructor, which takes one argument.

04:01.900 --> 04:03.730
Which is of type string.

04:03.730 --> 04:08.310
And then we are going to set this one as name, this dot name equal to this.

04:09.990 --> 04:13.560
Now if I call this function right?

04:14.510 --> 04:16.100
All this didn't.

04:17.120 --> 04:18.770
Function dot apply.

04:18.800 --> 04:25.120
Because apply is the method which we use in order to pass an input to the function functional interface.

04:25.140 --> 04:27.890
Then I'm going to pass the name as ABC.

04:28.130 --> 04:35.420
If I run, this is going to print the same object, meaning similar object, but name will have ABC

04:35.420 --> 04:36.440
instead of null.

04:38.980 --> 04:39.520
There you go.

04:39.520 --> 04:44.410
It printed the student object, but with the name as ABC.

04:45.680 --> 04:48.020
This is all about constructor reference.

04:48.470 --> 04:50.450
With this we came to the end of this tutorial.
