Access Level Modifiers3:55 with Craig Dennis
Access level modifiers such as public and private help to explain our intent.
All right, so we left a little problem in our PezDispencer class that 0:01 I want to go in and fix with you. 0:04 We made a field that exposes the character's name, and not 0:06 only can we access the field to get the current value, but we can also change it. 0:09 Now in real life, we can't really change the character of a PezDispenser 0:14 after it's been created, can we? 0:17 Yet, as we saw, 0:19 you can do exactly that with the newly created object we just made. 0:20 We never want people to misuse the object that we allowed them to create. 0:24 But it's kind of only us the blame, right? 0:27 We allowed that to happen. 0:30 We should make it as clear as possible to consumers of our class, 0:32 how we intend them to use it. 0:36 Now when I say consumers, 0:38 I mean the developers who are going to be using your code. 0:40 This might seem like a strange concept, but 0:44 it's one that I think is super important to grasp early on. 0:46 Are you ready? 0:49 Other people are going to use your code, they are. 0:51 They need to be able to look at your work when you aren't there to explain it, and 0:54 they will need to be able to understand how you were intending it to work. 0:58 And of course, sometimes that person is you. 1:02 Sometimes you forget what you were trying to convey. 1:05 It always behooves you to be more explicit, and 1:08 Java is very good at letting you express yourself. 1:11 It's part of this verbosity that makes things challenging in the beginning but 1:14 shines bright once you grasp the concepts. 1:17 So in order to communicate how you intend people to use your objects, 1:20 you can add some additional qualifications to your statements. 1:24 In Java these are referred to as access level modifiers. 1:27 There are keywords that you can add your field definition to 1:31 further specify who is intended to access the information. 1:34 A table similar to this is found in the Java SE tutorial, 1:38 I've linked to in the teacher's notes. 1:41 So our current field isn't using any of these modifiers, public, 1:43 protected or private. 1:47 So it falls under this row here that says no modifier. 1:48 Now we haven't gotten to packages just yet but you can think of that for now as 1:51 files in the same folder or directory, which is exactly what we're seeing. 1:55 In fact, why don't we remove what we haven't covered yet? 1:59 Now our code in the example file was able to access the field, 2:02 because it's in the same folder or package as the PezDispenser class. 2:06 So let's fix that. 2:10 In order to ensure that code in the same folder cannot access our field, 2:11 which of these should we use? 2:15 That's right, we should mark our field as private. 2:17 Let's do it. 2:19 Okay, so it's pretty straightforward to mark a field as private, all you need to 2:20 do is put the access level modifier before the type declaration, right? 2:24 So it's string type declaration here, so I wanna put private and that's it. 2:29 Now let's try and rerun our code, and remember here's our code here. 2:34 So, we're gonna say a clear and 2:38 javac Example.java and java Example. 2:43 And bam, our code doesn't even compile. 2:51 Do you see how explicit that is? 2:54 The code doesn't even work if you misuse it. 2:56 Now that's pretty powerful, right? 2:59 So here what this first error is saying, 3:00 is it's saying it has private access in the PezDispenser so it can't set it. 3:03 But, wait a second, there's a second error here. 3:08 It's not even accessing the value at all. 3:10 We can't even get the value. 3:12 [LAUGH] That's some pretty good protection, right? 3:14 So we've completely hidden the part of our state to anything at all except for 3:16 the class itself over here. 3:20 This is the only bit where this is accessible. 3:21 Nobody else knows about this, not even instances of that class. 3:25 Now this kind of hiding is used for very deliberate purposes. 3:30 This is called encapsulation and 3:34 will delve deeper into this concept as the course progresses. 3:36 But for now, how do I get access to that value? 3:39 I'd love to be able to at least show the current character. 3:42 Well, the most common way to expose a value but 3:45 not let someone set it is by adding a method. 3:49 So we best learn how to add methods. 3:52 Let's do that next. 3:54
You need to sign up for Treehouse in order to download course files.Sign up