6- Method References
Jun 27, 2026 00:00
· 3:46
· English
· Whisper Turbo
· 2 Speakers
This transcript expires today.
Upgrade for permanent storage →
Showing only
0:03
S…
Speaker 1 (6- Method References)
Sometimes all we do in a lambda expression is passing the parameter or
0:07
S…
Speaker 1 (6- Method References)
parameters to an existing method.
0:09
S…
Speaker 1 (6- Method References)
For example,
0:10
S…
Speaker 1 (6- Method References)
over here, we're simply passing this message to the print line method of
0:14
S…
Speaker 1 (6- Method References)
this object.
0:15
S…
Speaker 1 (6- Method References)
In these cases,
0:16
S…
Speaker 1 (6- Method References)
it's easier to reference this method directly.
0:19
S…
Speaker 1 (6- Method References)
Let me show you.
0:19
S…
Speaker 1 (6- Method References)
So,
0:20
S…
Speaker 1 (6- Method References)
here's the syntax.
0:22
S…
Speaker 1 (6- Method References)
We type the name of the class or the object that contains this method.
0:27
S…
Speaker 1 (6- Method References)
Then we type double colons followed by the name of the method without
0:31
S…
Speaker 1 (6- Method References)
parentheses because we don't want to call this method We just want to add a reference
0:35
S…
Speaker 1 (6- Method References)
to it.
0:35
S…
Speaker 1 (6- Method References)
For example,
0:36
S…
Speaker 1 (6- Method References)
if I want to rewrite this lambda expression using a method reference,
0:40
S…
Speaker 1 (6- Method References)
I would type greet Now what is the object
0:45
S…
Speaker 1 (6- Method References)
that contains the print line method?
0:46
S…
Speaker 1 (6- Method References)
It's this object,
0:48
S…
Speaker 1 (6- Method References)
right? So we type system that out then we add double colons
0:52
S…
Speaker 1 (6- Method References)
followed by the name of the method without parentheses so
0:58
S…
Speaker 1 (6- Method References)
this is a method reference,
0:59
S…
Speaker 1 (6- Method References)
and this code is identical to what we have over here.
1:02
S…
Speaker 2 (6- Method References)
Now,
1:03
S…
Speaker 1 (6- Method References)
with IntelliJ,
1:04
S…
Speaker 1 (6- Method References)
we can always convert this kind of lambda expressions to a method reference very
1:08
S…
Speaker 1 (6- Method References)
easily.
1:08
S…
Speaker 1 (6- Method References)
So we put the character on the body of the lambda,
1:11
S…
Speaker 1 (6- Method References)
press alt and enter,
1:12
S…
Speaker 1 (6- Method References)
and then replace lambda with method reference.
1:15
S…
Speaker 1 (6- Method References)
There you go.
1:16
S…
Speaker 2 (6- Method References)
Now,
1:17
S…
Speaker 1 (6- Method References)
with this method references,
1:19
S…
Speaker 1 (6- Method References)
we can reference static or instance methods in a class,
1:22
S…
Speaker 1 (6- Method References)
as well as constructors.
1:23
S…
Speaker 1 (6- Method References)
Let me show you a few examples.
1:25
S…
Speaker 1 (6- Method References)
So I'm going to delete these.
1:28
S…
Speaker 1 (6- Method References)
and add a static method in this class,
1:30
S…
Speaker 1 (6- Method References)
public static void print message.
1:34
S…
Speaker 1 (6- Method References)
Now the signature of this method matches the print
1:39
S…
Speaker 1 (6- Method References)
method of our printer interface,
1:40
S…
Speaker 1 (6- Method References)
okay?
1:41
S…
Speaker 1 (6- Method References)
So here we call the greet method,
1:44
S…
Speaker 1 (6- Method References)
we should pass a printer object,
1:47
S…
Speaker 1 (6- Method References)
so here we can use a lambda expression,
1:49
S…
Speaker 1 (6- Method References)
message,
1:50
S…
Speaker 1 (6- Method References)
we take it and pass it to this method.
1:54
S…
Speaker 1 (6- Method References)
Right again,
1:55
S…
Speaker 1 (6- Method References)
all we're doing here is passing the parameter to an existing method so we can
1:59
S…
Speaker 1 (6- Method References)
use a method reference here We type the name of the class that contains this method
2:03
S…
Speaker 1 (6- Method References)
in this case lambdas demo so Lambdas
2:08
S…
Speaker 1 (6- Method References)
demo then double colons followed by the name of the method There
2:13
S…
Speaker 1 (6- Method References)
you go or you can convert this using IntelliJ now
2:17
S…
Speaker 1 (6- Method References)
what if this was an instance method?
2:19
S…
Speaker 1 (6- Method References)
Let's see how it works.
2:22
S…
Speaker 1 (6- Method References)
So I'm going to create
2:25
S…
Speaker 1 (6- Method References)
a lambdasDemo object,
2:27
S…
Speaker 1 (6- Method References)
demo,
2:28
S…
Speaker 1 (6- Method References)
we set it to a new instance of the lambdasDemo class.
2:31
S…
Speaker 1 (6- Method References)
Now we want to call the greet method and pass a lambda expression
2:35
S…
Speaker 1 (6- Method References)
like this.
2:36
S…
Speaker 1 (6- Method References)
Message goes to demo dot,
2:39
S…
Speaker 1 (6- Method References)
here we want to call this instance method,
2:41
S…
Speaker 1 (6- Method References)
so print message.
2:43
S…
Speaker 2 (6- Method References)
Now,
2:44
S…
Speaker 1 (6- Method References)
to use a method reference,
2:46
S…
Speaker 1 (6- Method References)
we type the name of the object that contains this method,
2:49
S…
Speaker 1 (6- Method References)
demo,
2:50
S…
Speaker 1 (6- Method References)
double colons,
2:51
S…
Speaker 1 (6- Method References)
and print.
2:54
S…
Speaker 1 (6- Method References)
If we use IntelliJ,
2:55
S…
Speaker 1 (6- Method References)
we get the exact same result.
2:58
S…
Speaker 2 (6- Method References)
Now,
2:59
S…
Speaker 1 (6- Method References)
what about passing a value to a constructor?
3:01
S…
Speaker 1 (6- Method References)
Let's take a look.
3:02
S…
Speaker 1 (6- Method References)
So delete.
3:03
S…
Speaker 1 (6- Method References)
I'm going to add a constructor here.
3:05
S…
Speaker 1 (6- Method References)
In this constructor,
3:08
S…
Speaker 1 (6- Method References)
we want to add a string parameter.
3:10
S…
Speaker 2 (6- Method References)
Now,
3:11
S…
Speaker 1 (6- Method References)
let's say we call the greet method and pass
3:16
S…
Speaker 1 (6- Method References)
a message to the constructor of the LambdasDemo class.
3:20
S…
Speaker 1 (6- Method References)
How can we use a method reference
3:24
S…
Speaker 1 (6- Method References)
here? Well,
3:25
S…
Speaker 1 (6- Method References)
we type the name of the class
3:28
S…
Speaker 1 (6- Method References)
that is lambdas demo,
3:29
S…
Speaker 1 (6- Method References)
double colon,
3:30
S…
Speaker 1 (6- Method References)
and here we type new to represent the constructor.
3:34
S…
Speaker 1 (6- Method References)
If we use IntelliJ,
3:36
S…
Speaker 1 (6- Method References)
we get the exact same result.
3:38
S…
Speaker 1 (6- Method References)
So with method references,
3:40
S…
Speaker 1 (6- Method References)
we can write compact and easier to read lambda expressions.
This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy
Summary
Click Summarize to generate an AI summary of this transcript.
Summarizing...
Ask AI About This Transcript
Ask anything about this transcript — the AI will find relevant sections and answer.