1 00:00:00,000 --> 00:00:04,905 [MUSIC] 2 00:00:04,905 --> 00:00:09,430 All right, let's work on getting some more state represented in our object. 3 00:00:09,430 --> 00:00:13,600 I want to get some Pez in here so we can do its job dispense. 4 00:00:13,600 --> 00:00:16,707 But first, in case you aren't that big of a fan of Pez. 5 00:00:16,707 --> 00:00:21,300 You might not know some vital information that we're going to need for our coding. 6 00:00:21,300 --> 00:00:25,400 So Pez refills come in a packs of 12 like this. 7 00:00:25,400 --> 00:00:29,650 And that happens to be exactly the same count as what the standard Pez dispenser 8 00:00:29,650 --> 00:00:31,855 can contain and no more. 9 00:00:31,855 --> 00:00:36,160 12 is the maximum limit that our object can contain, and that limit won't change, 10 00:00:36,160 --> 00:00:38,650 it will remain constant. 11 00:00:38,650 --> 00:00:41,690 We should express this limitation in code before people start 12 00:00:41,690 --> 00:00:43,470 filling it up, sound good? 13 00:00:43,470 --> 00:00:43,990 Let's do this. 14 00:00:45,180 --> 00:00:48,310 So the vital information that we just talked about of how many Pez 15 00:00:48,310 --> 00:00:49,690 can be stored in dispenser. 16 00:00:49,690 --> 00:00:52,830 It's going to prove crucial to how our object works. 17 00:00:52,830 --> 00:00:56,140 Now, this number of Pez that can fit in a dispenser is something that will never 18 00:00:56,140 --> 00:00:57,390 change, right? 19 00:00:57,390 --> 00:01:01,190 Values that do not change are often referred to as constants. 20 00:01:01,190 --> 00:01:04,844 A naming convention that helps explain that a variable is a constant is to make 21 00:01:04,844 --> 00:01:08,003 all letters uppercase and separate the words with an underscore. 22 00:01:08,003 --> 00:01:12,310 So let's add a max pez constant to our class. 23 00:01:12,310 --> 00:01:14,119 So we'll just come up here. 24 00:01:14,119 --> 00:01:18,814 We'll say, public int MAX_PEZ, 25 00:01:18,814 --> 00:01:23,230 and like we said, that was 12. 26 00:01:23,230 --> 00:01:26,420 Okay, so let's imagine that this object gets created as a currently stands in our 27 00:01:26,420 --> 00:01:30,290 implementation MAX_PEZ is public. 28 00:01:30,290 --> 00:01:31,590 So as you know, 29 00:01:31,590 --> 00:01:35,760 anyone could change the limit of how many Pez this can be loaded with. 30 00:01:35,760 --> 00:01:38,560 That's not actually how things work in real life, right? 31 00:01:38,560 --> 00:01:40,969 I can't do that with an actual Pez dispenser. 32 00:01:40,969 --> 00:01:44,880 So again, we should make sure that our objects reflect reality. 33 00:01:44,880 --> 00:01:48,720 We can make it reflect reality with, you guessed it, our old friend final. 34 00:01:49,960 --> 00:01:51,269 So let's go ahead and save this. 35 00:01:51,269 --> 00:01:53,120 So let's poke at it in jshell really quick. 36 00:01:53,120 --> 00:01:55,424 So I'm gonna open up jshell down here. 37 00:01:55,424 --> 00:01:59,800 And jshell allows you to open classes, which is pretty cool. 38 00:01:59,800 --> 00:02:03,720 So to open your code, you just do a / and then the command open. 39 00:02:03,720 --> 00:02:08,702 And then you can just start typing your class and open PezDispenser.java. 40 00:02:08,702 --> 00:02:10,850 We're gonna open up as PezDipenser.java. 41 00:02:10,850 --> 00:02:14,840 So now, what that does is PezDispenser actually lives in the code here. 42 00:02:14,840 --> 00:02:18,241 So we can say, PezDispenser. 43 00:02:18,241 --> 00:02:21,074 And we'll just call it pd, that's bad, right? 44 00:02:21,074 --> 00:02:21,620 That's bad, what does PD mean? 45 00:02:21,620 --> 00:02:22,744 Letter, Police Department? 46 00:02:22,744 --> 00:02:24,274 I don't know. 47 00:02:24,274 --> 00:02:26,476 All right, so I'm gonna say, Yoda. 48 00:02:26,476 --> 00:02:30,358 Okay, and if we wanted to, now, 49 00:02:30,358 --> 00:02:34,670 we can see that the MAX_PEZ exists. 50 00:02:34,670 --> 00:02:38,626 MAX_PEZ, okay. And if we try to change that 51 00:02:38,626 --> 00:02:42,539 constant 123 in there. 52 00:02:42,539 --> 00:02:45,460 You can assign a value to find a variable, MAX_PEZ, cool. 53 00:02:45,460 --> 00:02:46,732 So we've blocked the value from changing. 54 00:02:46,732 --> 00:02:51,080 And our object is now this much closer to representing reality. 55 00:02:51,080 --> 00:02:53,920 Now, this concept of providing vital constant information 56 00:02:53,920 --> 00:02:55,542 about your class is super common. 57 00:02:55,542 --> 00:02:57,980 Here, let's take a look at the integer class actually. 58 00:02:57,980 --> 00:03:00,207 So if we got an integer, and I come here. 59 00:03:00,207 --> 00:03:02,815 Now, jshell like we said, offers tab completion. 60 00:03:02,815 --> 00:03:05,914 So let's leave Integer and then we'll do a tab. 61 00:03:05,914 --> 00:03:10,030 And look at all the uppercase values there, so here we got BYTES. 62 00:03:10,030 --> 00:03:14,400 We got MAX_VALUE, MIN_VALUE, so let's see what the max value of integer is. 63 00:03:17,680 --> 00:03:19,950 So that's how big a number can be stored in. 64 00:03:19,950 --> 00:03:20,613 But you know what? 65 00:03:20,613 --> 00:03:22,460 That's right off the class. 66 00:03:22,460 --> 00:03:26,390 And that's because it's true of every integer that will ever be created. 67 00:03:26,390 --> 00:03:28,340 Now, can we do that in our class? 68 00:03:28,340 --> 00:03:30,535 I see that we did it on our instance, right? 69 00:03:30,535 --> 00:03:31,820 But can we do it like this? 70 00:03:31,820 --> 00:03:37,939 Can we say, PezDispenser.MAX_PEZ. 71 00:03:37,939 --> 00:03:38,588 And it says, 72 00:03:38,588 --> 00:03:42,440 a non static variable MAX_PEZ cannot be a referenced from a static context. 73 00:03:43,760 --> 00:03:45,290 Nope, it doesn't work. 74 00:03:45,290 --> 00:03:47,950 But this constant is true of all PezDispensers that will 75 00:03:47,950 --> 00:03:50,040 ever be created from this class. 76 00:03:50,040 --> 00:03:51,710 So it should be defined on the class. 77 00:03:51,710 --> 00:03:53,867 We should need to create an instance. 78 00:03:53,867 --> 00:03:54,690 We should be able to act just like Integer did. 79 00:03:55,720 --> 00:03:58,510 So as you might have guessed by now, it's just another keyword. 80 00:03:58,510 --> 00:04:02,750 Now, the problem here is that the keyword isn't very clear as to what it's doing. 81 00:04:02,750 --> 00:04:04,700 The keyword is static. 82 00:04:04,700 --> 00:04:06,680 So let's put it up here and get it in here. 83 00:04:06,680 --> 00:04:09,500 So say static, I'm gonna save the file again. 84 00:04:10,690 --> 00:04:13,110 And then let's reopen our Pez dispenser. 85 00:04:13,110 --> 00:04:19,210 So if you say, open ns2p in our tab, PezDispenser.java. 86 00:04:19,210 --> 00:04:21,950 Cool, and I'm gonna control L to clear the screen here. 87 00:04:21,950 --> 00:04:29,453 And if I do PezDispenser.MAX_PEZ. 88 00:04:29,453 --> 00:04:33,636 Now, these are sometimes also referred to as class level variables because you can 89 00:04:33,636 --> 00:04:36,698 access them at the class level, as opposed to the instance. 90 00:04:36,698 --> 00:04:39,548 And the error from before, it makes a little bit more sense, right? 91 00:04:39,548 --> 00:04:40,112 It said, 92 00:04:40,112 --> 00:04:45,260 non-static variable MAX_PEZ cannot be referenced from a static context. 93 00:04:45,260 --> 00:04:50,800 Now, this class level is called the static context versus the instances context. 94 00:04:50,800 --> 00:04:51,923 Now, the keyword, 95 00:04:51,923 --> 00:04:55,901 static actually refers to how the variable itself is stored in memory. 96 00:04:55,901 --> 00:05:00,820 But what it actually allows you to do is the ability to provide variables and 97 00:05:00,820 --> 00:05:06,400 methods directly off your class as opposed to having them on the instance. 98 00:05:06,400 --> 00:05:07,970 So actually here, 99 00:05:07,970 --> 00:05:12,180 the auto complete in jshell will show you what's available statically, right? 100 00:05:12,180 --> 00:05:13,895 If we do Integer again and we press tab. 101 00:05:13,895 --> 00:05:17,120 There's one we've seen before, parseInt. 102 00:05:17,120 --> 00:05:20,740 So that's a great example of a static method that you've already used, right? 103 00:05:20,740 --> 00:05:29,440 So remember that it'll let you take a string parseInt and return the number. 104 00:05:29,440 --> 00:05:30,890 See, how it's right off the class? 105 00:05:30,890 --> 00:05:32,954 Now, I don't think we need any of those in our class. 106 00:05:32,954 --> 00:05:35,020 But I thought I'd show you since you've seen that already. 107 00:05:35,020 --> 00:05:37,170 And when I showed you before, I had said, 108 00:05:37,170 --> 00:05:40,622 there's a static method off of the Integer class named parseInt. 109 00:05:40,622 --> 00:05:43,930 And before that, probably just sounded like mumbo jumbo. 110 00:05:43,930 --> 00:05:46,950 But now, it should be starting to be more familiar. 111 00:05:46,950 --> 00:05:51,050 So now, if we pop back over to Example.java before anything, 112 00:05:51,050 --> 00:05:54,890 we can use it because it's now public and static, so let's do that. 113 00:05:54,890 --> 00:05:56,138 So we're making a PEZ Dispenser. 114 00:05:56,138 --> 00:05:59,283 So let's add a new System.out, 115 00:05:59,283 --> 00:06:04,464 we'll do a print line, or let's do printf actually. 116 00:06:04,464 --> 00:06:09,539 We'll say, ("FUN FACT: There are %d 117 00:06:09,539 --> 00:06:14,183 PEZ allowed in every dispenser",. 118 00:06:14,183 --> 00:06:18,173 And we'll go ahead, and 119 00:06:18,173 --> 00:06:23,493 we will push in our value before we 120 00:06:23,493 --> 00:06:29,205 create PezDispenser.MAX_PEZ. 121 00:06:29,205 --> 00:06:32,050 I should put a new line at the end of this. 122 00:06:35,580 --> 00:06:38,680 And see, how we didn't need to have the instance at all? 123 00:06:38,680 --> 00:06:40,750 This is before we initialized it. 124 00:06:40,750 --> 00:06:45,320 A fun fact about Java specifically is that because a variable is modified with static 125 00:06:45,320 --> 00:06:49,115 and final, when the compilation happens, when javac happens. 126 00:06:49,115 --> 00:06:53,055 It replaces all instances of that variable with the actual value. 127 00:06:53,055 --> 00:06:57,415 Okay, so now that we know how to not overfill these Pez Dispensers, 128 00:06:57,415 --> 00:07:00,735 let's make sure we don't overfill our brains with new concepts. 129 00:07:00,735 --> 00:07:04,405 Why don't you take a quick break and let this information sink in. 130 00:07:04,405 --> 00:07:05,320 Now, during your break, 131 00:07:05,320 --> 00:07:10,040 try to think of any other constants for objects in the real world. 132 00:07:10,040 --> 00:07:12,880 Are those constants unique to that specific object or 133 00:07:12,880 --> 00:07:15,100 all objects of that type? 134 00:07:15,100 --> 00:07:16,260 Okay, I'll see in a bit. 135 00:07:16,260 --> 00:07:18,039 Enjoy your break. 136 00:07:18,039 --> 00:07:23,512 We built this city, we built this city on rock and rule.