WEBVTT

00:00.510 --> 00:01.020
Hi everyone.

00:01.020 --> 00:02.310
Welcome back to this tutorial.

00:02.310 --> 00:08.580
In this tutorial we will discuss about the local variables and its restrictions with Lambda expressions.

00:09.470 --> 00:11.180
What are local variables?

00:11.300 --> 00:16.640
You might know what is a local variable already, but I would like to throw some light on this.

00:16.670 --> 00:22.550
Any variable that is declared inside a method is called a local variable.

00:22.970 --> 00:27.230
Lambda have some restrictions on using the local variables.

00:27.260 --> 00:29.540
To summarize, there are two restrictions.

00:29.570 --> 00:37.370
Number one, you are not allowed to use the same local variable name as a lambda parameter or inside

00:37.370 --> 00:38.510
the lambda body.

00:38.540 --> 00:46.310
Number two, you are not allowed to reassign a new value to a local variable in the lambda expression.

00:46.340 --> 00:49.700
One other thing is that there are no restrictions.

00:49.700 --> 00:53.530
For instance, variables in lambda expression.

00:53.540 --> 01:01.070
What does it mean is that you are allowed to modify or change a value of the instance variable.

01:01.100 --> 01:05.120
Let's understand this by coding some examples.

01:06.760 --> 01:09.760
Let me open IntelliJ and code this examples.

01:10.980 --> 01:11.420
There you go.

01:11.430 --> 01:12.270
I have the intelligence.

01:12.300 --> 01:12.690
Open.

01:12.690 --> 01:16.230
The first step is I'm going to create a class specific for this one.

01:16.440 --> 01:20.280
I'm going to name this one as a lambda variable one.

01:20.820 --> 01:25.500
Let me make this class executable by adding the public static void main method.

01:25.530 --> 01:32.850
The first restriction is that you're not allowed to use the local variable name in the lambda expression

01:32.850 --> 01:33.720
or body.

01:35.170 --> 01:38.870
Why not declare a local variable called int I equal to zero?

01:39.630 --> 01:42.060
Okay, I'm going to code a simple use case here.

01:42.080 --> 01:45.830
Basically consumer which takes in an integer.

01:46.520 --> 01:51.770
I'm going to name this one as C1 equal to let's import this consumer.

01:51.800 --> 01:52.310
There we go.

01:52.340 --> 01:53.870
The consumer is imported.

01:53.900 --> 02:00.950
What I'm going to do is I'm going to pass in I, I'm going to try to pass the I and then try to print

02:00.950 --> 02:01.730
this value.

02:07.320 --> 02:12.300
So if we take a look at it here, it has the compilation issue variable.

02:12.300 --> 02:15.180
I is already defined in this scope.

02:15.300 --> 02:17.010
So this is one of the restriction.

02:17.010 --> 02:23.550
You are not allowed to use the same variable which is already defined in this method scope.

02:23.550 --> 02:25.140
Here the method is main method.

02:25.170 --> 02:27.930
If I give this one as I1, then that is fine.

02:28.320 --> 02:30.630
Let's talk about the next restriction.

02:30.630 --> 02:33.510
So there is a local variable called int I equal to zero.

02:33.510 --> 02:40.920
If you try to declare the same variable like int i equal to two, this is not allowed because you have

02:40.920 --> 02:44.820
a local variable already defined in this method scope.

02:46.310 --> 02:47.660
There is a first restriction.

02:47.660 --> 02:53.480
Let's talk about the next restriction, which is trying to reassign a variable.

02:54.910 --> 02:55.960
The new value.

02:56.830 --> 02:58.870
So I'm going to create a class called.

03:00.160 --> 03:02.950
Lambda variable to.

03:05.630 --> 03:07.820
And then let's make this class executable.

03:09.380 --> 03:15.080
So what I'm going to do is I'm going to declare a variable called int value equal to four.

03:15.110 --> 03:18.470
So this is the local variable, right?

03:18.470 --> 03:22.340
And the next step is I'm going to declare a consumer interface.

03:23.240 --> 03:27.050
Of type integer and the name that one as C1.

03:27.560 --> 03:29.960
Let's import this consumer interface.

03:30.270 --> 03:33.320
Then I'm going to pass a value called I.

03:35.290 --> 03:40.450
So in here, what we are going to do, we are going to print the value of four.

03:42.980 --> 03:43.550
Right.

03:43.580 --> 03:47.480
No issues until this point or you can print the value of.

03:49.080 --> 03:50.850
The local variable value.

03:51.210 --> 03:56.700
So I'm going to call this 17. accept and pass the value as four.

03:56.880 --> 04:00.240
So this is going to perform some summation of.

04:01.180 --> 04:02.980
Value plus I and print the value.

04:03.020 --> 04:07.120
So if I run this one, it is going to print the value as eight.

04:07.270 --> 04:11.290
Now let's say you want to perform some operation on this one.

04:11.290 --> 04:14.740
Let's say you want to reassign the value like this.

04:14.740 --> 04:18.220
You want to increment the value whenever you call it here.

04:19.180 --> 04:21.060
There is a error, right?

04:21.070 --> 04:27.360
We got an error variable used in lambda expression should be final or effectively final.

04:27.370 --> 04:34.150
What does it mean is that this is a second restriction so you are not allowed to modify any value,

04:34.510 --> 04:39.730
meaning modify any local variable that is being referenced in the lambda scope.

04:39.850 --> 04:44.080
Let's say you want to change this value value equal to six.

04:44.110 --> 04:45.580
Even then, this is not allowed.

04:45.580 --> 04:50.050
Even though you are not modifying this value inside the lambda scope.

04:50.050 --> 04:54.130
Still, you are trying to modify this inside the method scope, right?

04:54.550 --> 04:56.080
That is also not allowed.

04:56.110 --> 05:01.300
Let's say you are going to move this one as an instance variable.

05:01.300 --> 05:04.510
I'm going to copy this line and then move it over here.

05:06.560 --> 05:11.450
And then I'm going to have this one increment the value.

05:11.480 --> 05:12.610
So what is the error?

05:12.620 --> 05:18.050
Create field value or make me non-static or make value static?

05:18.050 --> 05:21.440
So we are trying to access the value in the static context.

05:21.440 --> 05:26.150
That's the reason why you have to define it as a define it with a static keyword.

05:26.150 --> 05:30.290
So in here this is earns, this is an instance variable.

05:30.950 --> 05:32.930
In that case, there are no issues.

05:32.930 --> 05:38.630
So as I mentioned before in the presentation itself, for instance, variables or class variables,

05:38.630 --> 05:39.800
there are no restrictions.

05:39.800 --> 05:48.020
You are allowed to reassign or change or modify the value, but for local variables you are not allowed

05:48.020 --> 05:48.590
to do that.

05:48.590 --> 05:50.540
Let's run this example and then check.

05:50.630 --> 05:51.590
There you go.

05:51.590 --> 05:54.800
It incremented the value from 4 to 5.

05:54.830 --> 05:57.830
That's the reason why you see the result as nine.

06:00.480 --> 06:00.810
With this.

06:00.810 --> 06:02.480
We came to the end of this tutorial.

06:02.490 --> 06:07.830
Let's take a look at another concept called Effectively Final in the next tutorial.

06:08.070 --> 06:09.300
Thank you for watching.
