Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
You can protect your variables from re-assignment using the final keyword
Learn more
Okay, so over the past few lessons, 0:00 we have successfully picked up some language constructs to 0:02 explain that a character name is required to create a new Pez dispenser. 0:06 And after it's been created, you currently can't change that character, 0:10 which was exactly what we were intending to convey. 0:14 Because we're modeling what happens in real life. 0:17 Getting so close like we just talked about though. 0:20 What if someone on our team doesn't know that these heads can't be changed. 0:23 And they come in and write a new method that does just that, it changes the head. 0:28 No, we'd hopefully, catch that in a review of their code, but 0:32 we can do better than that. 0:35 Let's simulate the scenario and 0:37 I'll introduce a concept that will help solve the problem. 0:39 So let's go ahead and assume that a developer on our team, 0:42 let's call him Chris. 0:45 He grabbed our Pez dispenser class and decided to make a new method 0:47 that would change the character head and return the previous one. 0:50 So let's walk through that method creation really quick. 0:54 Let's see, obviously, Chris made that public, right? 0:57 He wanted somebody to use it because it's useful. 1:00 And like we said, it's gonna return the original character as the return value. 1:02 So that's a string, and then we'll name it. 1:06 Let's call it swapHead cuz it's gonna swap the heads up. 1:09 And it's callable so, we're gonna open parenthesis. 1:13 And it will declare a parameter of type string that represents a character name, 1:15 right? 1:21 So we're gonna ask that somebody passes in the new character name. 1:22 And then we're gonna open up the method body, and I'm gonna close it right away. 1:25 Uh-oh, we've got that naming collision problem again, right? 1:29 So look, here's a character name. 1:32 And we wanna swap out the character name, so 1:33 we know how to deal with that though, right? 1:35 So first, what we wanna do is get a hold of the original character name, 1:37 so that we can store it in a variable. 1:41 So let's be clear about it. 1:43 Let's say, string originalCharacterName. 1:45 And we want to make sure that we keep this character name. 1:51 So we're gonna say, this.charactername, not running into that naming collision. 1:55 We're not gonna fall for it, right? 1:59 Cuz this is in scope. 2:01 And now, what we'll do is we'll swap out this characterName with the characterName. 2:03 So basically, all we're doing is just keeping a safe copy 2:10 of what was originally in there because that's what we wanna return, right? 2:14 So we're gonna return the original, 2:17 [LAUGH] I keep on typing the originalCharacterName. 2:20 So now, if we walked over to this method, but we didn't want Krista, right? 2:27 We can show you how it's used. 2:34 So we're gonna say, 2:36 before = dispenser.swapHead. 2:39 And we're gonna put in bring back the Darth Lord again, Darth Vader. 2:44 And we're gonna print 2:50 out ("It was %s but 2:55 Chris switched it to %s. 3:00 And then we'll do a new one. 3:07 And so we want to first, let's first %s as before, right? 3:10 So before, it was what came out of the dispenser. 3:15 And then next, we're gonna say, dispenser. 3:18 And we're gonna call our own method getCharacterName, cool. 3:20 And we end that line. 3:25 All right, so I'm gonna save it. 3:27 And then I am going to say, 3:30 clear && javac Example.java && java Example. 3:32 What do you think, is it gonna work? 3:38 I think so, I think you did it. 3:40 Yep, so there it is. 3:44 It was yield up at Chris switched to Darth Vader. 3:45 Chris did exactly what we didn't want him to do. 3:47 He changed the head. 3:49 Come on, Chris. 3:51 But we can't really buy him, can we? 3:52 We didn't make it very clear. 3:54 We could have at least left a comment so he knew. 3:57 Then actually, there's something way better than a comment. 3:59 Something that would stop Chris and his tracks, even better. 4:02 It wouldn't even let him compile and that's the final keyword. 4:05 So when you want a variable to be assigned once and only once, 4:09 you market with the final key word, final. 4:14 So now, if we go and we try to run this code, we'll see that Chris got blocked. 4:20 You cannot assign a value to the final variable characterName, and 4:25 that is happening from inside line 14, right? 4:30 So the PezDispenser line 14 if we come in here to line 14. 4:33 Sorry, in the PezDispenser class [INAUDIBLE] line 14, 4:38 that's in the swapHead, okay? 4:41 Cuz we assigned it once here. 4:43 And he's trying to swap it here, it's telling that. 4:45 In a more advanced editor which we get to hear in a few courses. 4:48 This would have warned him very loudly before he even tried to compile. 4:51 So again, final here, says that this variable characterName is declared, 4:54 it can only be initialized once, it doesn't allow for any more assignments. 5:00 Now, you can use the final keyword for more than just field. 5:07 You can use methods in even classes. 5:09 Check the teacher's notes for more. 5:11 I'm gonna go ahead and say, sorry Chris, you can't do this, you're out of here. 5:13 Let's give her this. 5:17 And I'm gonna get rid of this line over here, because now, we have protected it. 5:18 And that code makes no sense, awesome. 5:24 This video is the final one in this stage. 5:27 And I know, I know, you're probably like, finally! 5:30 You're doing great. 5:33 And don't worry, I've been intentionally taking things slow and 5:34 making sure that you can read the intent of Java code when you come across it. 5:37 Also, now, you can write it. 5:41 Now, that we got the basics in place. 5:42 We've got fields, access modifiers, methods, and 5:45 constructors, let's pick up the pace a bit. 5:47 I think it's finally time for 5:50 us to start working through the core features of this Pez dispenser. 5:51 I know our little dispenser object isn't very impressive at the moment. 5:54 Let's do something to change that. 5:58 Let's wrap up this stage so that we can get some Pez in and out of this thing. 6:00 But first, let's do a quick practice review of what we just learned. 6:04
You need to sign up for Treehouse in order to download course files.
Sign up