1 00:00:01,060 --> 00:00:04,420 All right, so we left a little problem in our PezDispenser class that 2 00:00:04,420 --> 00:00:06,390 I want to go in and fix with you. 3 00:00:06,390 --> 00:00:09,560 We made a field that exposes the character's name, and not 4 00:00:09,560 --> 00:00:14,030 only can we access the field to get the current value, but we can also change it. 5 00:00:14,030 --> 00:00:17,650 Now in real life, we can't really change the character of a PezDispenser 6 00:00:17,650 --> 00:00:19,420 after it's been created, can we? 7 00:00:19,420 --> 00:00:20,890 Yet, as we saw, 8 00:00:20,890 --> 00:00:24,640 you can do exactly that with the newly created object we just made. 9 00:00:24,640 --> 00:00:27,870 We never want people to misuse the object that we allowed them to create. 10 00:00:27,870 --> 00:00:30,570 But it's kind of only us to blame, right? 11 00:00:30,570 --> 00:00:32,580 We allowed that to happen. 12 00:00:32,580 --> 00:00:36,390 We should make it as clear as possible to consumers of our class, 13 00:00:36,390 --> 00:00:37,710 how we intend them to use it. 14 00:00:38,870 --> 00:00:40,780 Now when I say consumers, 15 00:00:40,780 --> 00:00:44,290 I mean the developers who are going to be using your code. 16 00:00:44,290 --> 00:00:46,570 This might seem like a strange concept, but 17 00:00:46,570 --> 00:00:49,220 it's one that I think is super important to grasp early on. 18 00:00:49,220 --> 00:00:50,440 Are you ready? 19 00:00:51,480 --> 00:00:53,310 Other people are going to use your code, they are. 20 00:00:54,920 --> 00:00:58,540 They need to be able to look at your work when you aren't there to explain it, and 21 00:00:58,540 --> 00:01:02,860 they will need to be able to understand how you were intending it to work. 22 00:01:02,860 --> 00:01:05,510 And of course, sometimes that person is you. 23 00:01:05,510 --> 00:01:08,630 Sometimes you forget what you were trying to convey. 24 00:01:08,630 --> 00:01:11,010 It always behooves you to be more explicit, and 25 00:01:11,010 --> 00:01:14,150 Java is very good at letting you express yourself. 26 00:01:14,150 --> 00:01:17,720 It's part of this verbosity that makes things challenging in the beginning but 27 00:01:17,720 --> 00:01:20,920 shines bright once you grasp the concepts. 28 00:01:20,920 --> 00:01:24,270 So in order to communicate how you intend people to use your objects, 29 00:01:24,270 --> 00:01:26,740 you can add some additional qualifications to your statements. 30 00:01:27,830 --> 00:01:31,990 In Java these are referred to as access level modifiers. 31 00:01:31,990 --> 00:01:34,740 There are keywords that you can add your field definition to 32 00:01:34,740 --> 00:01:38,350 further specify who is intended to access the information. 33 00:01:38,350 --> 00:01:41,510 A table similar to this is found in the Java SE tutorial, 34 00:01:41,510 --> 00:01:42,540 I've linked to in the teacher's notes. 35 00:01:43,690 --> 00:01:47,070 So our current field isn't using any of these modifiers, public, 36 00:01:47,070 --> 00:01:48,130 protected or private. 37 00:01:48,130 --> 00:01:51,500 So it falls under this row here that says no modifier. 38 00:01:51,500 --> 00:01:55,990 Now we haven't gotten to packages just yet but you can think of that for now as 39 00:01:55,990 --> 00:01:59,320 files in the same folder or directory, which is exactly what we're seeing. 40 00:01:59,320 --> 00:02:02,260 In fact, why don't we remove what we haven't covered yet? 41 00:02:02,260 --> 00:02:06,060 Now our code in the example file was able to access the field, 42 00:02:06,060 --> 00:02:10,180 because it's in the same folder or package as the PezDispenser class. 43 00:02:10,180 --> 00:02:11,310 So let's fix that. 44 00:02:11,310 --> 00:02:15,490 In order to ensure that code in the same folder cannot access our field, 45 00:02:15,490 --> 00:02:17,250 which of these should we use? 46 00:02:17,250 --> 00:02:19,520 That's right, we should mark our field as private. 47 00:02:19,520 --> 00:02:20,510 Let's do it. 48 00:02:20,510 --> 00:02:24,350 Okay, so it's pretty straightforward to mark a field as private, all you need to 49 00:02:24,350 --> 00:02:29,960 do is put the access level modifier before the type declaration, right? 50 00:02:29,960 --> 00:02:34,754 So it's String type declaration here, so I wanna put private and that's it. 51 00:02:34,754 --> 00:02:38,689 Now let's try and rerun our code, and remember here's our code here. 52 00:02:38,689 --> 00:02:43,702 So, we're gonna say a clear and 53 00:02:43,702 --> 00:02:50,023 javac Example.java and java Example. 54 00:02:51,802 --> 00:02:54,880 And bam, our code doesn't even compile. 55 00:02:54,880 --> 00:02:56,870 Do you see how explicit that is? 56 00:02:56,870 --> 00:02:59,170 The code doesn't even work if you misuse it. 57 00:02:59,170 --> 00:03:00,730 Now that's pretty powerful, right? 58 00:03:00,730 --> 00:03:03,000 So here what this first error is saying, 59 00:03:03,000 --> 00:03:08,370 is it's saying it has private access in the PezDispenser so it can't set it. 60 00:03:08,370 --> 00:03:10,460 But, wait a second, there's a second error here. 61 00:03:10,460 --> 00:03:12,860 It's not even accessing the value at all. 62 00:03:12,860 --> 00:03:14,395 We can't even get the value. 63 00:03:14,395 --> 00:03:16,930 [LAUGH] That's some pretty good protection, right? 64 00:03:16,930 --> 00:03:20,330 So we've completely hidden the part of our state to anything at all except for 65 00:03:20,330 --> 00:03:21,870 the class itself over here. 66 00:03:21,870 --> 00:03:25,580 This is the only bit where this is accessible. 67 00:03:25,580 --> 00:03:30,790 Nobody else knows about this, not even instances of that class. 68 00:03:30,790 --> 00:03:34,800 Now this kind of hiding is used for very deliberate purposes. 69 00:03:34,800 --> 00:03:36,530 This is called encapsulation and 70 00:03:36,530 --> 00:03:39,540 will delve deeper into this concept as the course progresses. 71 00:03:39,540 --> 00:03:42,320 But for now, how do I get access to that value? 72 00:03:42,320 --> 00:03:45,710 I'd love to be able to at least show the current character. 73 00:03:45,710 --> 00:03:49,420 Well, the most common way to expose a value but 74 00:03:49,420 --> 00:03:52,750 not let someone set it is by adding a method. 75 00:03:52,750 --> 00:03:54,460 So we best learn how to add methods. 76 00:03:54,460 --> 00:03:55,050 Let's do that next.