5- Variable Capture
Jun 26, 2026 23:24
· 1:56
· English
· Whisper Turbo
· 2 Wasemaji
Rekodi hiyo yamalizika leo.
Kufugwa kwa ajili ya uhifadhi wa kudumu →
Kuonyesha tu
0:03
S…
Speaker 2 (5- Variable Capture)
In the body of this lambda,
0:05
S…
Speaker 2 (5- Variable Capture)
we're using the message parameter that we have declared over here.
0:08
S…
Speaker 2 (5- Variable Capture)
But we can also access the local variables in the enclosing method.
0:12
S…
Speaker 2 (5- Variable Capture)
So I'm going to declare a variable called prefix and
0:16
S…
Speaker 2 (5- Variable Capture)
set it to hyphen.
0:17
S…
Speaker 1 (5- Variable Capture)
Now,
0:18
S…
Speaker 2 (5- Variable Capture)
here we can reference prefix and then append the message.
0:22
S…
Speaker 1 (5- Variable Capture)
So when we run,
0:23
S…
Speaker 1 (5- Variable Capture)
we get hyphen hello world.
0:26
S…
Speaker 2 (5- Variable Capture)
We can also access the static fields in the enclosing class.
0:29
S…
Speaker 2 (5- Variable Capture)
So I'm going to move this from here and declare it as a static
0:34
S…
Speaker 2 (5- Variable Capture)
field in this class.
0:35
S…
Speaker 2 (5- Variable Capture)
Public static string prefix.
0:37
S…
Speaker 1 (5- Variable Capture)
Set it to hyphen.
0:39
S…
Speaker 1 (5- Variable Capture)
Now we can access it in our lambda expression.
0:42
S…
Speaker 2 (5- Variable Capture)
So when we run,
0:44
S…
Speaker 2 (5- Variable Capture)
we get the same result as before.
0:46
S…
Speaker 2 (5- Variable Capture)
We can also access the instance fields.
0:49
S…
Speaker 2 (5- Variable Capture)
So I'm going to make this an instance field.
0:52
S…
Speaker 2 (5- Variable Capture)
and also make the show method an instance method.
0:55
S…
Speaker 1 (5- Variable Capture)
So as you can see,
0:57
S…
Speaker 2 (5- Variable Capture)
we can access the prefix field.
0:58
S…
Speaker 1 (5- Variable Capture)
Now,
0:59
S…
Speaker 2 (5- Variable Capture)
what about this?
1:01
S…
Speaker 2 (5- Variable Capture)
What do you think this represents here?
1:04
S…
Speaker 2 (5- Variable Capture)
It represents the current instance of the lambdas demo class.
1:08
S…
Speaker 2 (5- Variable Capture)
So if we type dot,
1:10
S…
Speaker 2 (5- Variable Capture)
you can see the prefix field and the show method.
1:13
S…
Speaker 2 (5- Variable Capture)
So this is one of the differences between lambda expressions and anonymous
1:17
S…
Speaker 2 (5- Variable Capture)
inner classes.
1:18
S…
Speaker 1 (5- Variable Capture)
In lambda expressions,
1:20
S…
Speaker 2 (5- Variable Capture)
this references the enclosing object,
1:22
S…
Speaker 2 (5- Variable Capture)
whereas in anonymous inner classes,
1:24
S…
Speaker 2 (5- Variable Capture)
this references the current instance of the anonymous inner class.
1:28
S…
Speaker 2 (5- Variable Capture)
Another difference between these two types is that anonymous inner classes can
1:33
S…
Speaker 1 (5- Variable Capture)
have state.
1:33
S…
Speaker 2 (5- Variable Capture)
So they can have fields to store some data in lambda expressions.
1:37
S…
Speaker 2 (5- Variable Capture)
We cannot have fields because this lambda expression is just representing
1:41
S…
Speaker 2 (5- Variable Capture)
an anonymous function.
1:42
S…
Speaker 2 (5- Variable Capture)
So we cannot have instance fields here.
1:45
S…
Speaker 2 (5- Variable Capture)
We cannot have state.
1:45
S…
Speaker 2 (5- Variable Capture)
We can only access the local variables declared in the enclosing object
1:50
S…
Speaker 2 (5- Variable Capture)
as well as the static and instance fields in the enclosing class.
Nakala hii ilitokezwa na AI (utambuaji wa usemi wa kiaya). Inaweza kuwa na makosa Équipe dhidi ya sauti ya awali kwa utumizi wa kuchambua. Sera ya AI
Muhtasari
Bonyeza muhtasari ili kutokeza muhtasari wa AI juu ya nakala hii.
Kutoa muhtasari...
Uliza Maswali Kuhusu Mpito Huu
Uliza jambo lolote kuhusu nakala hii ya kitabu KULEA ile nitakayopata sehemu zinazofaa na majibu.