Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Filling the Dispenser7:17 with Craig Dennis
Let's create a method that changes the internal state of our object
All right. 0:00 Now, we know how many that Pez we can put in our dispenser. 0:01 We know our limits. 0:04 And we could realistically and safely add some. 0:04 Thinking through these limits is always a good practice to fall into as you 0:07 start modeling objects. 0:10 Now, let's see. 0:12 We wanna store some state, right? 0:13 We wanna store, how many Pez are actually in the dispenser, right? 0:14 So that's an infield, right? 0:19 And we will name that pezCount. 0:20 And we don't really want anyone to just change this willy nilly. 0:24 So a good practice for 0:29 that is to keep it private until you have a reason to expose it to your consumers. 0:30 So we'll make this private. 0:36 Said another way, if you aren't sure, 0:38 always start with private switching to public only when you have a need. 0:40 You'll thank yourself later in the future, I promise. 0:44 We'll continue exploring this concept more throughout our journey. 0:46 So for now, let's just leave this undeclared but uninitialized. 0:49 And in our constructor, we'll set it. 0:54 So where did that go again? 0:56 Here, right. It's got the same name as the class. 0:58 And it doesn't have a return value. 1:02 So, what we'll do is just like when you buy a new dispenser, it's empty, right? 1:03 You have to put that candy in, so we'll set it to zero. 1:08 So we'll say, pezCount = zero. 1:10 And I don't have to use this because it's not in scope. 1:14 It's talking about this pezCount, cool. 1:16 So now, when you create a new Pez dispenser, it'll be empty. 1:20 But you obviously, wanna fill it up, so let's do that. 1:24 Let's make a new method called fill. 1:26 So it's definitely something that we wanna expose, right? 1:28 So it's public. 1:31 And what should it return? 1:33 Nothing really, right? 1:35 So in Java, when a method doesn't return anything, we market with the keyword void. 1:36 V-O-I-D, void, and we'll call fill. 1:42 And it doesn't take any parameters. 1:46 And so let's just imagine that we have a package of Pez, 1:48 which as we said is the same amount of the dispenser can hold. 1:52 So we have that constant, so let's use it. 1:55 So we'll say, the pezCount = MAX_PEZ, okay? 1:57 So we'll save it, and we'll flip over to our example program. 2:04 And we'll have it called the new film method. 2:08 So we'll say, System.out, and we'll call printLine. 2:10 And we'll say, ("Filling the dispenser with 2:17 delicious, Pez. 2:22 So it doesn't really return anything, so we can just call it, right? 2:28 We'll say, dispenser.fill. 2:32 And let's go ahead and run that. 2:35 So we'll say, clear && javac Example.java && java Example. 2:37 Filling the dispenser with delicious Pez, and it's filled. 2:48 We haven't really exposed how to check if there is any in there. 2:51 But we can trust that it did, but we probably should do that, right? 2:53 Let's make a method. 2:57 But first, let's look one more time at that method that we had, the Fill Method. 2:58 Let's come over here. 3:03 So Fill is public, right? 3:03 So we call that off of the instance method. 3:05 And we need to specify what type of object a method returns. 3:08 And just like we did here, right? 3:12 We said that this returns a string but this doesn't return anything. 3:13 So we said, void and that means that nothing will happen from this method. 3:17 And in fact, Java will block me if I try to come in here. 3:22 And we'll say, #jklol and 3:26 I try to run it one more time. 3:30 The compiler will stop and it'll say, incompatible types, 3:36 unexpected return value, cool. 3:39 We get the error at compiler time because our method is returning a string, 3:41 even though we specifically expressed, it will return nothing. 3:45 And we did that by using the void keyword. 3:48 So let's remove that statement and everything should be fine again. 3:50 Okay, so where were we? 3:55 Sorry, right. 3:56 So then we set the private field pezCount to the maximum amount, 3:58 which we have in a constant, which is common to all classes MAX_PEZ, awesome. 4:02 So now, we've got our Pez dispenser filled with the maximum amount 4:07 of Pez that it can hold. 4:11 We used a static and final variable to create a constant. 4:12 And we named it in such a way that people looking at it can realize that it 4:16 is a constant. 4:20 Again, that's all uppercase and separated by underscores. 4:21 So let's take a minute to discuss the benefit 4:25 of the fill method that we just created. 4:28 So the exact process of filling our dispenser is tucked away and 4:30 out of sight of the consumer of our object. 4:34 Its implementation code is kept in our class. 4:37 And we've only exposed the single method fill. 4:40 Because what the code is actually doing is essentially hidden from the caller, 4:43 we are free to change the way that we are maintaining state internally. 4:48 For instance, instead of just keeping count like we did, 4:52 we could actually create little Pez candy objects stored in each of the 12 slots. 4:55 But for now, keeping a counter is fine for a current needs. 5:00 By not exposing the internal state, 5:04 we've successfully encapsulate it or kept private. 5:06 How the insides of our object actually work? 5:09 All the consumer knows is that when they call fill, the dispenser will be filled. 5:12 They don't really care how. 5:17 This is called abstraction. 5:19 We've abstracted away the need for 5:21 the consumers of our code to understand how we actually fill our dispenser. 5:24 This obstruction concept is one of the main benefits for actually using objects. 5:29 And it's the basis of Object Oriented Programming or OOP. 5:33 This abstraction isn't unique to programming though, right? 5:38 It's present in every day objects. 5:41 Let's return to that real world radio example from before, right? 5:43 Now, I know that I can change the channel on the radio, but 5:47 I'm really not sure what happens when I do that, like step by step. 5:49 All I know is that the radio starts playing the station of the channel I 5:53 set it to. 5:56 It works exactly like I expect it to, and 5:57 I am using it the way that has been exposed to me. 5:59 I have absolutely no idea of how that works internally. 6:03 And that's really none of my business. 6:07 That's the business of the factory that produced that specific radio. 6:08 I bet there's a maximum channel that I can't turn to pass, right? 6:11 Now, if I wanted to, I could bust out a screwdriver, open it up and 6:15 take a look at how it was all actually working. 6:19 Instead of taking that time to understand the internals, 6:22 I just use what the object has publicly exposed, and I'm happy. 6:25 And I'm most likely dancing off or lead to some sweet tunes. 6:28 You know what? 6:32 While I was in that example Java file, I noticed something pretty cool. 6:33 That Java boilerplate that we're using to kick off our programs? 6:36 It's starting to have fewer unknowns in it. 6:40 Here, come take a peek. 6:42 When you run the command java Example, what happens is that it searches for 6:44 a class with a public static method named main. 6:48 Look, there's the public, there's the static. 6:51 And it returns nothing, there's the void. 6:53 So basically, more or less, what is happening is it calls Example.main(). 6:55 You can pass an argument on the command line where we've been kicking these off 6:59 from, that's what this RS is talking about that. 7:02 We'll go over that here, pretty soon. 7:04 Pretty awesome that you can almost read all of that now, isn't it? 7:07 Let's do an exercise reviewing this stuff. 7:10 And then jump back into learning even more ways to express ourselves using objects. 7:12
You need to sign up for Treehouse in order to download course files.Sign up