1 00:00:00,980 --> 00:00:03,450 Okay, so we have a problem. 2 00:00:03,450 --> 00:00:07,220 Every Pez dispenser we create is currently hard coded to Yoda. 3 00:00:07,220 --> 00:00:10,380 Now, hard coded meaning it's permanently set to a value. 4 00:00:10,380 --> 00:00:12,890 So that's not really what we want, right? 5 00:00:12,890 --> 00:00:16,270 We want to allow that character to be configurable, but 6 00:00:16,270 --> 00:00:18,950 really only at creation time. 7 00:00:18,950 --> 00:00:22,850 We'd like to allow consumers of our code to create whatever character type of 8 00:00:22,850 --> 00:00:26,010 Pez dispenser they want when they create a new one, but 9 00:00:26,010 --> 00:00:27,952 we don't want it to change after it's created. 10 00:00:29,140 --> 00:00:33,830 The common solution to this problem is to create what is known as a constructor. 11 00:00:33,830 --> 00:00:38,330 A constructor is a method that will run when you instantiate the class. 12 00:00:38,330 --> 00:00:42,570 Again instantiation happens when you create the new instance of the class 13 00:00:42,570 --> 00:00:44,570 using the new keyword. 14 00:00:44,570 --> 00:00:48,570 A constructor is a great place to stick required information about the initial set 15 00:00:48,570 --> 00:00:49,230 up of the object. 16 00:00:50,260 --> 00:00:54,130 Let's go create a constructor for our Pez dispenser class that allows a consumer to 17 00:00:54,130 --> 00:00:58,062 specify the character name upon creation of a new Pez dispenser object. 18 00:01:00,370 --> 00:01:05,550 >> Okay, so like I said, constructors look and behave a whole lot like methods. 19 00:01:05,550 --> 00:01:11,480 They have an access modifier, so we'll set that to public, say public. 20 00:01:11,480 --> 00:01:15,780 And what identifies them as a constructor as opposed to a method is that there is 21 00:01:15,780 --> 00:01:18,620 no return type, but then it's followed by the name of the class. 22 00:01:18,620 --> 00:01:22,822 So we're gonna, the name of the class as PezDispenser, so PezDispenser, right. 23 00:01:22,822 --> 00:01:25,030 The name of the class is PezDispenser, 24 00:01:25,030 --> 00:01:28,910 and the name of our constructor is PezDispenser there, so. 25 00:01:28,910 --> 00:01:32,110 And then we'll add parentheses to make sure that this callable, 26 00:01:32,110 --> 00:01:33,850 just like we do for methods, right. 27 00:01:33,850 --> 00:01:36,410 Remember we made the parentheses here so it looks like a method. 28 00:01:36,410 --> 00:01:39,600 And now we need to define our parameters like we did with string contains. 29 00:01:39,600 --> 00:01:43,459 Now these describe to our caller what is expected to be passed in. 30 00:01:43,459 --> 00:01:47,990 So we want the creator of our object to specify the name. 31 00:01:47,990 --> 00:01:49,510 So let's add that as a parameter. 32 00:01:49,510 --> 00:01:50,840 So we're gonna say, String name. 33 00:01:52,360 --> 00:01:54,990 So that should be a nice clear understandable name. 34 00:01:54,990 --> 00:01:58,593 And then we're gonna close that off and we're gonna open up our curly brace to 35 00:01:58,593 --> 00:02:01,272 start a new scope, and I'm gonna immediately close it. 36 00:02:01,272 --> 00:02:05,208 And inside of here we want to set the private field 37 00:02:05,208 --> 00:02:09,050 character name to what was passed in, right. 38 00:02:09,050 --> 00:02:14,340 So we're gonna say, CharacterName = Name. 39 00:02:14,340 --> 00:02:19,525 The constructor just like the method has access to these private member fields. 40 00:02:19,525 --> 00:02:24,105 Since we've now required the name to be set from the caller of the class, let's go 41 00:02:24,105 --> 00:02:28,415 ahead and remove this Yoda instantiation here, and we'll just close that. 42 00:02:28,415 --> 00:02:31,081 So now the variable has been declared but not initialized. 43 00:02:31,081 --> 00:02:34,605 All right, so I'm going to save this. 44 00:02:34,605 --> 00:02:41,955 And if I flip back to example.Java we can see that the code will not compile. 45 00:02:44,209 --> 00:02:48,175 So it says constructor PezDispenser in class PezDispenser cannot be applied to 46 00:02:48,175 --> 00:02:49,440 the given types. 47 00:02:49,440 --> 00:02:52,080 So it required a string, but it found no arguments. 48 00:02:52,080 --> 00:02:55,272 Actual formal argument links differ, right, 49 00:02:55,272 --> 00:02:58,233 because we specified that we needed a name. 50 00:02:58,233 --> 00:03:02,409 And the reason why it worked before is because if no constructors are defined, 51 00:03:02,409 --> 00:03:07,040 a default constructor that takes no parameters is automatically assumed. 52 00:03:07,040 --> 00:03:10,740 However, once at least one constructor is defined, 53 00:03:10,740 --> 00:03:15,660 like we just did, you must use one of those explicitly defined constructors. 54 00:03:15,660 --> 00:03:18,030 Okay, so let's do that, let's let's push one in. 55 00:03:18,030 --> 00:03:22,620 So let's fill out this with Donatello, 56 00:03:22,620 --> 00:03:26,250 my favorite hero in a half shell, and see if it works. 57 00:03:26,250 --> 00:03:27,418 So let's run that again. 58 00:03:30,247 --> 00:03:31,680 There he is. 59 00:03:31,680 --> 00:03:38,472 All right, let's switch this back to Yoda, and save it, and we can run it. 60 00:03:41,724 --> 00:03:42,910 Excellent. 61 00:03:42,910 --> 00:03:44,980 All right, so let's walk this one more time. 62 00:03:44,980 --> 00:03:48,915 So what we've done is we've declared a constructor, and 63 00:03:48,915 --> 00:03:53,589 we know it's a constructor because it has the same name as the class. 64 00:03:53,589 --> 00:04:00,700 And we've required users to pass in a string argument for name. 65 00:04:00,700 --> 00:04:02,430 And so they pass that in and it comes in here, 66 00:04:02,430 --> 00:04:05,940 it sets the character name, which is this private field here. 67 00:04:05,940 --> 00:04:09,260 Now when I did this originally, I cheated a little bit, and 68 00:04:09,260 --> 00:04:13,480 I made the parameter name a little bit different than what was passed in. 69 00:04:13,480 --> 00:04:17,840 Now I did this to avoid what is known as a naming collision because I 70 00:04:17,840 --> 00:04:20,500 was afraid of the confusion that it might have introduced. 71 00:04:20,500 --> 00:04:24,100 But as a wise Yoda would tell us, fear leads to anger. 72 00:04:24,100 --> 00:04:25,590 So let's deal with the problem. 73 00:04:25,590 --> 00:04:31,180 So really this parameter should be CharacterName, right? 74 00:04:31,180 --> 00:04:35,970 I mean, people who look at our code might actually get angry because they wonder 75 00:04:35,970 --> 00:04:40,833 what name means, especially when we have a getter that gets the character name, 76 00:04:40,833 --> 00:04:41,349 right. 77 00:04:42,420 --> 00:04:45,520 It would be unclear what this was asking for if it still said name. 78 00:04:45,520 --> 00:04:49,890 So we've renamed it, but this brings up a weird problem, right? 79 00:04:49,890 --> 00:04:54,680 So the variable that comes in here called CharacterName, let's just go ahead and 80 00:04:54,680 --> 00:04:58,740 switch this too, these variables now called CharacterName but 81 00:04:58,740 --> 00:05:03,610 we already have something in our scope called character name, right. 82 00:05:03,610 --> 00:05:06,497 So how do we know which one of these it's talking about? 83 00:05:06,497 --> 00:05:10,060 There is a way to be more explicit. 84 00:05:10,060 --> 00:05:12,095 Inside of a method's body, or 85 00:05:12,095 --> 00:05:15,590 constructor's body, you can use the keyword this. 86 00:05:15,590 --> 00:05:18,450 So now this is talking about this instance. 87 00:05:18,450 --> 00:05:20,058 That's what this means, so 88 00:05:20,058 --> 00:05:23,575 this.characterName is talking about this character name. 89 00:05:23,575 --> 00:05:26,325 This.characterName is the private field and 90 00:05:26,325 --> 00:05:30,030 CharacterName here that's outside of it is just called CharacterName. 91 00:05:30,030 --> 00:05:34,582 Now, this exact fear of confusion around naming collision is not new and 92 00:05:34,582 --> 00:05:38,030 there have been many attempts at trying to be more explicit because of it. 93 00:05:38,030 --> 00:05:40,600 There was at one time a very popular style of 94 00:05:40,600 --> 00:05:45,160 prefixing your private member variables with a lower case m. 95 00:05:45,160 --> 00:05:49,240 So what we would actually name or variable would be something like this. 96 00:05:49,240 --> 00:05:53,510 And this is popular in Android programming so 97 00:05:53,510 --> 00:05:55,628 mCharacterName would look like this, right. 98 00:05:55,628 --> 00:05:57,930 So in that saying this is a member of variable. 99 00:05:57,930 --> 00:06:03,100 And what that allows you to do is then change this to be mCharacterName and 100 00:06:03,100 --> 00:06:07,530 then change this getter here to say mCharacterName. 101 00:06:07,530 --> 00:06:12,180 So it's very clear that you're talking about these private member variables. 102 00:06:13,190 --> 00:06:16,730 So this is a common yet different enough solution 103 00:06:16,730 --> 00:06:20,496 that it gives me a chance to bring up a new point which is one of coding style. 104 00:06:20,496 --> 00:06:23,770 Now companies often settle on a coding style for projects, and you should always 105 00:06:23,770 --> 00:06:27,780 follow what is in place, no matter if you believe differently or not. 106 00:06:27,780 --> 00:06:30,740 You can definitely try to make your point heard, but 107 00:06:30,740 --> 00:06:33,080 always code in the style of what's been determined by your team. 108 00:06:33,080 --> 00:06:35,660 So if somebody is using this in prefix style of coding, 109 00:06:35,660 --> 00:06:38,485 you should follow that and you can see how clear it is. 110 00:06:38,485 --> 00:06:43,050 Now I'm sure that there are people who would definitely disagree quite loudly 111 00:06:43,050 --> 00:06:44,085 with this coding style. 112 00:06:44,085 --> 00:06:47,630 And as you've seen the problem can actually be 113 00:06:47,630 --> 00:06:51,200 solved by using language constructs of this keyword. 114 00:06:51,200 --> 00:06:55,420 So there's very little suffering because we dealt with this fear initially. 115 00:06:55,420 --> 00:06:59,260 Now let's lean on everybody's favorite editor tool and get this backed out. 116 00:06:59,260 --> 00:07:03,210 I'm going to use Command Z, and you'll see that it will start undoing what I've done. 117 00:07:03,210 --> 00:07:04,185 So let's get this back. 118 00:07:04,185 --> 00:07:09,110 So there's this.characterName and boom, now we're back to there. 119 00:07:09,110 --> 00:07:12,620 Now we should be all set with our Yoda approved constructor. 120 00:07:12,620 --> 00:07:13,120 There we go. 121 00:07:14,330 --> 00:07:16,240 >> Awesome, so now we've made it so 122 00:07:16,240 --> 00:07:21,550 a Pez dispenser cannot be created without specifying which character it is. 123 00:07:21,550 --> 00:07:25,740 And, after it's created, we haven't exposed anyway to change it. 124 00:07:25,740 --> 00:07:29,410 So we're pretty safe from the character changing at all after it's been created. 125 00:07:30,840 --> 00:07:34,240 Now I say pretty safe because as long as it's just 126 00:07:34,240 --> 00:07:37,930 us in control of our class file source code, if we don't create a method 127 00:07:37,930 --> 00:07:41,390 that allows us to change the character name, it won't change. 128 00:07:41,390 --> 00:07:44,340 But we very well could be working with a team or, 129 00:07:44,340 --> 00:07:48,520 like I said earlier, maybe we'll forget that specific business case of 130 00:07:48,520 --> 00:07:50,270 character heads don't change after creation. 131 00:07:51,350 --> 00:07:55,050 We can actually make things even more specific so 132 00:07:55,050 --> 00:07:58,530 that you can't change it even within the same class definition. 133 00:07:58,530 --> 00:08:00,060 Let's do that right after this quick break.