Hi everyone.
Welcome back to this tutorial.
In this tutorial we will discuss about the local variables and its restrictions with Lambda expressions.
What are local variables?
You might know what is a local variable already, but I would like to throw some light on this.
Any variable that is declared inside a method is called a local variable.
Lambda have some restrictions on using the local variables.
To summarize, there are two restrictions.
Number one, you are not allowed to use the same local variable name as a lambda parameter or inside
the lambda body.
Number two, you are not allowed to reassign a new value to a local variable in the lambda expression.
One other thing is that there are no restrictions.
For instance, variables in lambda expression.
What does it mean is that you are allowed to modify or change a value of the instance variable.
Let's understand this by coding some examples.
Let me open IntelliJ and code this examples.
There you go.
I have the intelligence.
Open.
The first step is I'm going to create a class specific for this one.
I'm going to name this one as a lambda variable one.
Let me make this class executable by adding the public static void main method.
The first restriction is that you're not allowed to use the local variable name in the lambda expression
or body.
Why not declare a local variable called int I equal to zero?
Okay, I'm going to code a simple use case here.
Basically consumer which takes in an integer.
I'm going to name this one as C1 equal to let's import this consumer.
There we go.
The consumer is imported.
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
this value.
So if we take a look at it here, it has the compilation issue variable.
I is already defined in this scope.
So this is one of the restriction.
You are not allowed to use the same variable which is already defined in this method scope.
Here the method is main method.
If I give this one as I1, then that is fine.
Let's talk about the next restriction.
So there is a local variable called int I equal to zero.
If you try to declare the same variable like int i equal to two, this is not allowed because you have
a local variable already defined in this method scope.
There is a first restriction.
Let's talk about the next restriction, which is trying to reassign a variable.
The new value.
So I'm going to create a class called.
Lambda variable to.
And then let's make this class executable.
So what I'm going to do is I'm going to declare a variable called int value equal to four.
So this is the local variable, right?
And the next step is I'm going to declare a consumer interface.
Of type integer and the name that one as C1.
Let's import this consumer interface.
Then I'm going to pass a value called I.
So in here, what we are going to do, we are going to print the value of four.
Right.
No issues until this point or you can print the value of.
The local variable value.
So I'm going to call this 17. accept and pass the value as four.
So this is going to perform some summation of.
Value plus I and print the value.
So if I run this one, it is going to print the value as eight.
Now let's say you want to perform some operation on this one.
Let's say you want to reassign the value like this.
You want to increment the value whenever you call it here.
There is a error, right?
We got an error variable used in lambda expression should be final or effectively final.
What does it mean is that this is a second restriction so you are not allowed to modify any value,
meaning modify any local variable that is being referenced in the lambda scope.
Let's say you want to change this value value equal to six.
Even then, this is not allowed.
Even though you are not modifying this value inside the lambda scope.
Still, you are trying to modify this inside the method scope, right?
That is also not allowed.
Let's say you are going to move this one as an instance variable.
I'm going to copy this line and then move it over here.
And then I'm going to have this one increment the value.
So what is the error?
Create field value or make me non-static or make value static?
So we are trying to access the value in the static context.
That's the reason why you have to define it as a define it with a static keyword.
So in here this is earns, this is an instance variable.
In that case, there are no issues.
So as I mentioned before in the presentation itself, for instance, variables or class variables,
there are no restrictions.
You are allowed to reassign or change or modify the value, but for local variables you are not allowed
to do that.
Let's run this example and then check.
There you go.
It incremented the value from 4 to 5.
That's the reason why you see the result as nine.
With this.
We came to the end of this tutorial.
Let's take a look at another concept called Effectively Final in the next tutorial.
Thank you for watching.