WEBVTT

00:00.900 --> 00:01.470
Hi everyone.

00:01.470 --> 00:02.830
Welcome back to this tutorial.

00:02.850 --> 00:05.820
In this tutorial we'll talk about method reference.

00:06.750 --> 00:10.320
We will be discussing the theory part as part of this tutorial.

00:11.390 --> 00:14.480
This concept got introduced as part of Java eight.

00:14.690 --> 00:21.560
The sole purpose of method reference is to simplify the implementation of functional interfaces.

00:21.710 --> 00:25.370
You might be wondering, we already did that by using lambdas.

00:25.400 --> 00:30.140
Lambdas helped us to move away from creating anonymous inner classes.

00:30.200 --> 00:36.470
Yes, you are right, but this one is going to simplify the lambda expression itself.

00:37.600 --> 00:43.450
And also call method reference as a shortcut for the lambda expressions from the name itself.

00:43.460 --> 00:47.780
It is clear that this one is used to refer a method in a class.

00:47.800 --> 00:50.650
Let's take a look at the syntax of the method reference.

00:51.580 --> 00:52.930
The syntax is class.

00:52.930 --> 00:56.470
Name a double colon operator followed by the method name.

00:56.500 --> 01:01.900
The right hand side references to the method name and the left hand side references to the class name.

01:02.580 --> 01:06.090
Let's talk about the different options that are presented here.

01:06.120 --> 01:10.230
First example is a class name followed by the instance method name.

01:10.260 --> 01:15.000
The second example is a class name, followed by the static method name in that class.

01:15.180 --> 01:20.040
And then the third example is an instance of the class, followed by the method name.

01:20.070 --> 01:25.110
Let's take a look at some of the examples where the method reference is applicable.

01:26.060 --> 01:32.450
Metal reference can be used wherever the lambda expression is referring to the method directly.

01:33.900 --> 01:35.550
Let's take a look at one of the example.

01:35.550 --> 01:39.450
This is one of the example which we coded in our prior tutorials.

01:39.480 --> 01:45.930
Here what happens is we are taking an input as a string and then we are applying the to uppercase on

01:45.930 --> 01:46.500
top of it.

01:46.860 --> 01:49.990
Basically, this is a function functional interface implementation.

01:50.010 --> 01:52.470
The input is a string and output is a string.

01:52.500 --> 01:55.710
How can we rewrite this using the method reference?

01:56.070 --> 01:58.560
Take a look at the method reference implementation.

01:58.740 --> 02:01.590
We give the class name as if you remember the syntax.

02:01.590 --> 02:04.470
The syntax is class name double colon operator.

02:04.470 --> 02:06.760
Followed by that we provide the method name.

02:06.780 --> 02:12.390
So the syntax is going to be the string, which is a class name and followed by the double colon and

02:12.390 --> 02:17.100
then the two uppercase method, because that's what we are trying to achieve as part of this lambda

02:17.100 --> 02:17.850
expression.

02:18.770 --> 02:24.020
So using method reference, we can write simpler and more readable code.

02:24.940 --> 02:29.620
Let's take a look at some of the examples where the method reference is not applicable.

02:31.120 --> 02:32.590
Take a look at an example.

02:32.800 --> 02:34.690
We take a look at this example here.

02:34.690 --> 02:36.460
We wrote our own logic.

02:36.610 --> 02:42.660
We are not referencing to any method that is implementing this predicate functional interface.

02:42.670 --> 02:49.060
In these kind of use cases, it is difficult to write a method reference for this one, but there is

02:49.060 --> 02:55.270
a workaround in solving these kind of problems and writing a method reference for these kind of codes

02:55.270 --> 02:55.690
too.

02:55.780 --> 03:01.630
We will get into those kind of examples and we will implement those in our next tutorial.

03:01.810 --> 03:04.980
But now this is all about method reference.

03:04.990 --> 03:07.310
With this we came to the end of this tutorial.

03:07.330 --> 03:08.410
Thank you for watching.
