alright i'm continuing on if I just run this again so we can see what we finished
in the last video so we got the same result as we did with the other commented out
code so basically we got to write less code to accomplish what we want to
do and instead of passing anonymous instance of upper concat we passed the
lambda instead now we could have done in only one line of code if we passed the
lambda expression directly to do string stuff rather than saving it to a variable
first I just want to show you that we can assign lambdas to variables and use
them later and if you want to use that same lambda in more than one place we
only have to define it once and then we can use it wherever we need it now if i
our lambda contained more than one statement then we would have to use the
return keyword let's change the body of our lambda so that stores the result of
the uppercase in concatenation into a new string variable and then returns it
just so you can see what that looks like i'm going back into our lambda on line 51
and we're going to actually put a code block there so I'm going to put first code
block there and it's going to be string.....
.....
....and everything else is
unchanged that's how you would execute multiple things in the body of the
lambda so again just a few things to note here first of all I used curly
braces
that's because the body now has more than one statement for the same reason
we also need the semicolons after each statement in the body
you may also notice that we added that when we added the curly braces but
before we tack on the string result equals IntelliJ complained that we no
longer had a statement once we add curly braces the return keyword is required
even if there's only one statement in the body just to confirm will run it to
make sure we get the same results still got the same result that that's good so
we seen several examples of lambda expressions instead of using the
upper concat lambda expression within the main method
that's actually create a class that isn't static so that we can take a look at
lambda expression and scope we're going to add the new class into the main java
file let's get into the bottom and will add that down here and we'll call this
class another class.....let's create a method here....
.....
....
.....
....
....all right in our main method
we are going to construct an instance of another class and call the do something
method and print out the so come back to our main class and what i might do
is actually delete some of this code and just comment it out for now just
in case you need to refer to it so let's call our another class so another
class.....
....so lets run that
we got string one and string two concatenated together that's because we're calling
the do something method that's creating an anonymous class to return s . one up
to uppercase plus s2 . uppercase ok so let's add another line to the upper
and concat method in the anonymous class and what we're going to do before we return
the string result we're gonna print out the class name of the anonymous
class instance will do the same for the another class instance so I'm going to
come down here and in the do something method before we do the return we're going to
print out.....
.....
....
ok so when we run that we should see that anonymous class name get the
output but in theory the anonymous sorry the another class class name gets printed
out and just move this down to the bottom so you can see a little bit better
so the in another class class name is another class and the anonymous classes
name is nothing and that's correct because of course the anonymous class
doesn't have a name which makes sense but let's actually use a lambda
expression instead of an anonymous class we're going to close this run window
let's use this lambda expression so we're going to do that so in the do something
method just going to comment this code out so we're clear
now we're going to do....
...
....
.....
....
....alright so what
do you think the name of the lambda expressions class is going to be so
let's actually run and find out we run it you can see that we got the lambda
expressions name is set to another class just like the another classes name
so what does that actually telling us well its telling us that the lambda or
that a lambda expression isn't a class when the code runs an anonymous
instance isn't created instead the lambda is treated like a nest block of
code and it has the same scope as a nested block so let's look at a few
examples to see this in action just so hopefully it becomes a bit clearer
so first let's look at a nested block that isn't part that isn't a part of the
lambda so instead of using a lambda in the do something method we've do it the long way
by using an anonymous
class will also put all the code inside a nested block so lets go back and do
that so i'm going to comment that out for now and so we just add a code block
here in the do something method will put....
....
.....so by putting the code inside a nested block all we mean is that we've
enclosed it with another set of curly braces as you can see I've done braces
on the line 105 and the line 115 now code within a nested block can reference variables
defined within the enclosing block meaning the block of code that contains
it which in this case is the do something method body if you were to define a
local variable in the enclosing block we could use it within the nested block so
let's actually do that just to confirm it by adding an int variable called count
and initializing to 0 and we're going to increment the value inside the nested
block and printed out so come up come up and do that before the final
return so just after the print out here so we need to do is just add our variable before
the code block outside of the code block so...
.....
....
....so if we run this should work without any errors you can see
we got i equals 1 clearly the increment worked and we got our result so basically the code
within the nested block can use the local variable I because nested blocks are
in the enclosing block scope
but what about the anonymous class though we already know that if we want to
reference a local variable defined outside the anonymous class we have to declare the
local variables as final if we try and use I inside the upper and concat method
will actually get a compile error
let's try to print the value of i from within the methods to confirm that does
happen i'm talking about this upper concat method on line 109 so we try
to print something before the return like....
....looking at that IntelliJ is complaining that needs to be
final
so let's add the final in front of the int declaration on line 105 so
final and the other thing we have to do is we'll have to change the code or
basically comment out the two lines outside the anonymous class that
update I because we can't change the value of a final variable once it has been
assigned so if we just come down here so just that one line anyway comment
that line out to avoid that being an error so now if we run that and obviously
that's set to 0 because it's a final variable and there's no recommendation
no incrementing of the value but the question here is why do local variables
have to be declared as final when we use them within an anonymous class that's
because the local variable doesn't belong to the anonymous class instance
what actually happens under the covers
is that the variable is replaced by whatever the value of i is when the
instance is constructed so it's possible we may not use the instance of an
anonymous class for a while we may even pass it to a method in another class and
there'd be no way for the java runtime to update the value within the anonymous
class instance every time it changed within the do something method
so in other words the values would get out of sync so for that reason the
values of local variables declared outside the scope of the anonymous class
and not allowed to change they have to be declared as final alright so let's get
back to how this relates to lambda expressions so we'll go back to using a
lambda in the do something method
we're gonna remove the extra set of curly braces as well and we'll also
remove the final keyword from the local variable declaration let's go ahead and
do that going to remove the final here going to remove the extra
curly braces put the code back space there and we'll put our
lambda instead of the other method so we type it again because it's good practice to
become familiar with them....
....
.....
....
....alright
let's just confirm that this works like it was working before string one string
2 in the screen you can see that it is working
alright I'll finish this video now in the next video
let's actually see how lambda expressions are treated like nested
blocks i'll see you in the next video