1 00:00:00,820 --> 00:00:02,974 Okay, so over the past few lessons, 2 00:00:02,974 --> 00:00:06,449 we have successfully picked up some language constructs to 3 00:00:06,449 --> 00:00:10,700 explain that a character name is required to create a new Pez dispenser. 4 00:00:10,700 --> 00:00:14,320 And after it's been created, you currently can't change that character, 5 00:00:14,320 --> 00:00:17,105 which was exactly what we were intending to convey. 6 00:00:17,105 --> 00:00:20,010 Because we're modeling what happens in real life. 7 00:00:20,010 --> 00:00:23,961 Getting so close like we just talked about though. 8 00:00:23,961 --> 00:00:28,470 What if someone on our team doesn't know that these heads can't be changed. 9 00:00:28,470 --> 00:00:32,871 And they come in and write a new method that does just that, it changes the head. 10 00:00:32,871 --> 00:00:35,855 No, we'd hopefully, catch that in a review of their code, but 11 00:00:35,855 --> 00:00:37,560 we can do better than that. 12 00:00:37,560 --> 00:00:39,205 Let's simulate the scenario and 13 00:00:39,205 --> 00:00:42,720 I'll introduce a concept that will help solve the problem. 14 00:00:42,720 --> 00:00:45,490 So let's go ahead and assume that a developer on our team, 15 00:00:45,490 --> 00:00:47,010 let's call him Chris. 16 00:00:47,010 --> 00:00:50,760 He grabbed our Pez dispenser class and decided to make a new method 17 00:00:50,760 --> 00:00:54,510 that would change the character head and return the previous one. 18 00:00:54,510 --> 00:00:57,210 So let's walk through that method creation really quick. 19 00:00:57,210 --> 00:01:00,209 Let's see, obviously, Chris made that public, right? 20 00:01:00,209 --> 00:01:02,648 He wanted somebody to use it because it's useful. 21 00:01:02,648 --> 00:01:06,100 And like we said, it's gonna return the original character as the return value. 22 00:01:06,100 --> 00:01:09,140 So that's a string, and then we'll name it. 23 00:01:09,140 --> 00:01:13,438 Let's call it swapHead cuz it's gonna swap the heads up. 24 00:01:13,438 --> 00:01:15,771 And it's callable so, we're gonna open parenthesis. 25 00:01:15,771 --> 00:01:21,786 And it will declare a parameter of type string that represents a character name, 26 00:01:21,786 --> 00:01:22,410 right? 27 00:01:22,410 --> 00:01:25,450 So we're gonna ask that somebody passes in the new character name. 28 00:01:25,450 --> 00:01:29,635 And then we're gonna open up the method body, and I'm gonna close it right away. 29 00:01:29,635 --> 00:01:32,670 Uh-oh, we've got that naming collision problem again, right? 30 00:01:32,670 --> 00:01:33,773 So look, here's a character name. 31 00:01:33,773 --> 00:01:35,566 And we wanna swap out the character name, so 32 00:01:35,566 --> 00:01:37,430 we know how to deal with that though, right? 33 00:01:37,430 --> 00:01:41,150 So first, what we wanna do is get a hold of the original character name, 34 00:01:41,150 --> 00:01:43,078 so that we can store it in a variable. 35 00:01:43,078 --> 00:01:45,133 So let's be clear about it. 36 00:01:45,133 --> 00:01:47,394 Let's say, string originalCharacterName. 37 00:01:51,010 --> 00:01:55,610 And we want to make sure that we keep this character name. 38 00:01:55,610 --> 00:01:59,850 So we're gonna say, this.charactername, not running into that naming collision. 39 00:01:59,850 --> 00:02:01,738 We're not gonna fall for it, right? 40 00:02:01,738 --> 00:02:03,560 Cuz this is in scope. 41 00:02:03,560 --> 00:02:10,720 And now, what we'll do is we'll swap out this characterName with the characterName. 42 00:02:10,720 --> 00:02:14,180 So basically, all we're doing is just keeping a safe copy 43 00:02:14,180 --> 00:02:17,260 of what was originally in there because that's what we wanna return, right? 44 00:02:17,260 --> 00:02:20,239 So we're gonna return the original, 45 00:02:20,239 --> 00:02:24,342 [LAUGH] I keep on typing the originalCharacterName. 46 00:02:27,339 --> 00:02:34,260 So now, if we walked over to this method, but we didn't want Krista, right? 47 00:02:34,260 --> 00:02:36,412 We can show you how it's used. 48 00:02:36,412 --> 00:02:39,629 So we're gonna say, 49 00:02:39,629 --> 00:02:44,823 before = dispenser.swapHead. 50 00:02:44,823 --> 00:02:50,442 And we're gonna put in bring back the Darth Lord again, Darth Vader. 51 00:02:50,442 --> 00:02:55,366 And we're gonna print 52 00:02:55,366 --> 00:03:00,029 out ("It was %s but 53 00:03:00,029 --> 00:03:05,740 Chris switched it to %s. 54 00:03:07,980 --> 00:03:08,860 And then we'll do a new one. 55 00:03:10,820 --> 00:03:15,170 And so we want to first, let's first %s as before, right? 56 00:03:15,170 --> 00:03:18,090 So before, it was what came out of the dispenser. 57 00:03:18,090 --> 00:03:20,584 And then next, we're gonna say, dispenser. 58 00:03:20,584 --> 00:03:25,800 And we're gonna call our own method getCharacterName, cool. 59 00:03:25,800 --> 00:03:27,470 And we end that line. 60 00:03:27,470 --> 00:03:30,137 All right, so I'm gonna save it. 61 00:03:30,137 --> 00:03:32,966 And then I am going to say, 62 00:03:32,966 --> 00:03:38,636 clear && javac Example.java && java Example. 63 00:03:38,636 --> 00:03:40,000 What do you think, is it gonna work? 64 00:03:40,000 --> 00:03:42,170 I think so, I think you did it. 65 00:03:44,210 --> 00:03:45,190 Yep, so there it is. 66 00:03:45,190 --> 00:03:47,870 It was yield up at Chris switched to Darth Vader. 67 00:03:47,870 --> 00:03:49,845 Chris did exactly what we didn't want him to do. 68 00:03:49,845 --> 00:03:51,001 He changed the head. 69 00:03:51,001 --> 00:03:52,470 Come on, Chris. 70 00:03:52,470 --> 00:03:54,980 But we can't really buy him, can we? 71 00:03:54,980 --> 00:03:57,110 We didn't make it very clear. 72 00:03:57,110 --> 00:03:59,750 We could have at least left a comment so he knew. 73 00:03:59,750 --> 00:04:02,870 Then actually, there's something way better than a comment. 74 00:04:02,870 --> 00:04:05,800 Something that would stop Chris and his tracks, even better. 75 00:04:05,800 --> 00:04:09,280 It wouldn't even let him compile and that's the final keyword. 76 00:04:09,280 --> 00:04:14,254 So when you want a variable to be assigned once and only once, 77 00:04:14,254 --> 00:04:19,130 you market with the final key word, final. 78 00:04:20,200 --> 00:04:25,722 So now, if we go and we try to run this code, we'll see that Chris got blocked. 79 00:04:25,722 --> 00:04:30,225 You cannot assign a value to the final variable characterName, and 80 00:04:30,225 --> 00:04:33,710 that is happening from inside line 14, right? 81 00:04:33,710 --> 00:04:38,717 So the PezDispenser line 14 if we come in here to line 14. 82 00:04:38,717 --> 00:04:41,933 Sorry, in the PezDispenser class [INAUDIBLE] line 14, 83 00:04:41,933 --> 00:04:43,850 that's in the swapHead, okay? 84 00:04:43,850 --> 00:04:45,238 Cuz we assigned it once here. 85 00:04:45,238 --> 00:04:48,270 And he's trying to swap it here, it's telling that. 86 00:04:48,270 --> 00:04:51,210 In a more advanced editor which we get to hear in a few courses. 87 00:04:51,210 --> 00:04:54,743 This would have warned him very loudly before he even tried to compile. 88 00:04:54,743 --> 00:05:00,950 So again, final here, says that this variable characterName is declared, 89 00:05:00,950 --> 00:05:07,610 it can only be initialized once, it doesn't allow for any more assignments. 90 00:05:07,610 --> 00:05:09,938 Now, you can use the final keyword for more than just field. 91 00:05:09,938 --> 00:05:11,720 You can use methods in even classes. 92 00:05:11,720 --> 00:05:13,290 Check the teacher's notes for more. 93 00:05:13,290 --> 00:05:17,300 I'm gonna go ahead and say, sorry Chris, you can't do this, you're out of here. 94 00:05:17,300 --> 00:05:18,218 Let's give her this. 95 00:05:18,218 --> 00:05:24,880 And I'm gonna get rid of this line over here, because now, we have protected it. 96 00:05:24,880 --> 00:05:27,880 And that code makes no sense, awesome. 97 00:05:27,880 --> 00:05:30,233 This video is the final one in this stage. 98 00:05:30,233 --> 00:05:33,280 And I know, I know, you're probably like, finally! 99 00:05:33,280 --> 00:05:34,380 You're doing great. 100 00:05:34,380 --> 00:05:37,380 And don't worry, I've been intentionally taking things slow and 101 00:05:37,380 --> 00:05:41,410 making sure that you can read the intent of Java code when you come across it. 102 00:05:41,410 --> 00:05:42,928 Also, now, you can write it. 103 00:05:42,928 --> 00:05:45,440 Now, that we got the basics in place. 104 00:05:45,440 --> 00:05:47,730 We've got fields, access modifiers, methods, and 105 00:05:47,730 --> 00:05:50,010 constructors, let's pick up the pace a bit. 106 00:05:50,010 --> 00:05:51,240 I think it's finally time for 107 00:05:51,240 --> 00:05:54,810 us to start working through the core features of this Pez dispenser. 108 00:05:54,810 --> 00:05:58,140 I know our little dispenser object isn't very impressive at the moment. 109 00:05:58,140 --> 00:06:00,280 Let's do something to change that. 110 00:06:00,280 --> 00:06:04,110 Let's wrap up this stage so that we can get some Pez in and out of this thing. 111 00:06:04,110 --> 00:06:06,980 But first, let's do a quick practice review of what we just learned.