Method References5:21 with Craig Dennis
Methods can be referred to using the `::` syntax.
Okay. 0:00 So let's take a look at this from a slightly different angle. 0:01 So our forEach method is looking for 0:06 a function that accepts a string and returns nothing. 0:08 And what we've done is just that. 0:14 We've made a function that accepts a string and it doesn't return anything. 0:16 This is just printing out there. 0:21 And so we take that string and 0:24 we put it into a function that accepts a string and returns nothing. 0:26 Sounds familiar doesn't it? 0:32 The signature of the consumer function 0:34 is the same signature as the standard print line method. 0:36 It takes a string and returns nothing. 0:41 Now, that's exactly what our function's doing is taking the string and 0:44 passing it right into that one, right? 0:47 It's doing the same thing. 0:48 So I'm gonna ask you to bend your brain a bit here. 0:50 Now, what if we were able to say, psst, hey! 0:54 Hey, forEach, why don't you just take each of the ingredients and 0:57 pass them directly into the println method? 1:02 We're ready for that, you can. 1:06 You just do this, you say ingredients.forEach(System.out) and 1:08 then you do two colons, println, to talk about that method. 1:17 So watch, that's exactly what this is suggesting here. 1:23 We're gonna go where it says, can be replaced with a method reference. 1:25 And here we go, let's do it. 1:30 Replace lambda with method reference, and boom. 1:32 It's the same thing. 1:34 So, what a method reference means is this referring to an instance method 1:36 on the static out instance object of system. 1:42 There's a method called print line which we use all the time. 1:48 And it's on an instance method that's static on the system. 1:50 But it's an instance method because the signature is exactly 1:54 what the 4H method is looking for. 1:58 We were able to just pass it in. 2:00 Now, I understand that this feeling is a little bit strange for your brain. 2:03 So here, let's do this. 2:08 I'm gonna go ahead, let's clean this up. 2:09 Let's just go ahead, and I'm gonna get rid of this. 2:11 All this stuff here, let's clean this up a bit. 2:14 Let's make sure we're still running. 2:17 And let's go ahead, and we'll make a new static method on our class here. 2:20 So it will be public, static, we'll make it not return anything so void right? 2:26 And let's make the method yell out to the consult so 2:33 we'll call it EL and it's gonna accept a string of words. 2:38 And whatever's passed in, we will just make it yell out, right? 2:46 So we'll do a printf, and we'll drop a placeholder. 2:50 A %s, we'll do some exclamation points, and then we'll do a new on it. 2:56 And what we wanna put in here, is we wanna do whatever words was passed in, and 3:01 we'll say toUpperCase. 3:05 Okay, cool. 3:08 So let's just make sure that our thing works. 3:09 Just give it a try. 3:12 So we'll say static method on Application.yell. 3:14 So it's called application, this is called Main. 3:18 So Main, this file that we're in here, .yell. 3:22 And we're gonna do what my daughter does when I tell her that something 3:27 has eggs in it. 3:31 She'll say, but I want a cupcake. 3:32 I want that cupcake. 3:34 And if we run this we'll see there she is yelling. 3:38 But I want that cupcake. 3:42 So now let's look at the method signature here. 3:44 It takes a string and it returns nothing, just like what for each is looking for. 3:47 So, therefore we should be able to just refer to that method. 3:52 Let's give it a try. 3:57 So I'm going to say ingredient for each. 3:59 I am gonna say main, that's this class here, and if you look here, it's yell. 4:03 Let's do that, and I'm gonna com dot this, and there we are yelling the ingredients. 4:09 This method reference, is referring to the static method yell on the main class. 4:14 Now, one thing I want to point out here which you might have caught is the yell 4:21 method is static. 4:24 However, this print lime method that we're using before is an instance method out 4:27 on system is static, but print line Is an instant method of that print stream. 4:32 Note how the syntax is exactly the same, 4:38 it was able to infer what contact the core things though. 4:41 We'll come back to this as we go a bit deeper. 4:45 In fact, let's go ahead and add that to the parking for right now. 4:47 Let's, that's called, 4:50 the way that that's figuring that out is called method reference inference. 4:52 We will come back and take a look at that. 4:58 Pretty cool and in synched. 5:01 So as long as a method or 5:03 lambda function matches the expected signature, you can use it as an argument. 5:05 Now, this is super powerful. 5:11 So these signatures are often called function shapes and we'll go take a look 5:13 at the basic ones we have available to us right after this quick break. 5:17
You need to sign up for Treehouse in order to download course files.Sign up