1 00:00:00,800 --> 00:00:05,220 Okay so we've fixed our problem with restricting changes to our character name, 2 00:00:05,220 --> 00:00:07,120 but it's a little too restrictive. 3 00:00:07,120 --> 00:00:09,950 It can't even be accessed from outside the class itself. 4 00:00:09,950 --> 00:00:13,840 We want to allow consumers of our objects to still be able to see the value, 5 00:00:13,840 --> 00:00:15,800 even if they can't change it. 6 00:00:15,800 --> 00:00:18,640 So how do we let them get the value then, you ask? 7 00:00:18,640 --> 00:00:19,590 Great question. 8 00:00:19,590 --> 00:00:23,065 Well the answer is we build a method that exposes a value. 9 00:00:23,065 --> 00:00:27,301 You've used methods before but for instance on the console object use 10 00:00:27,301 --> 00:00:31,190 the print F method or on strings we use the two lower case method. 11 00:00:31,190 --> 00:00:35,205 Now remember what I said that developers modeling the real world objects focus on 12 00:00:35,205 --> 00:00:38,490 two main characteristics which are state and behavior. 13 00:00:38,490 --> 00:00:41,660 Well fields usually are used to express that state and 14 00:00:41,660 --> 00:00:44,350 its methods help us to express that behavior. 15 00:00:44,350 --> 00:00:47,030 So, therefore you can kind of think of fields as characteristics about your 16 00:00:47,030 --> 00:00:50,780 object and methods as the verbs or actions that your object can perform. 17 00:00:51,840 --> 00:00:55,760 So, just like how you've use methods on objects before, like string contains or 18 00:00:55,760 --> 00:00:58,270 integer percent, it's time that we learn how to write our own. 19 00:00:59,480 --> 00:01:00,980 Okay, so let's do this. 20 00:01:00,980 --> 00:01:04,640 Let's add a method to our object that behaves like this. 21 00:01:04,640 --> 00:01:08,780 When the method is used or called it will respond with or 22 00:01:08,780 --> 00:01:10,940 return to the calling code. 23 00:01:10,940 --> 00:01:14,330 The value of the private field character name. 24 00:01:14,330 --> 00:01:18,955 Now defining a method is pretty similar More to the way that we define a field. 25 00:01:18,955 --> 00:01:21,205 They also have access modifiers. 26 00:01:21,205 --> 00:01:22,425 So let's come here. 27 00:01:22,425 --> 00:01:23,405 So when I come in here and 28 00:01:23,405 --> 00:01:25,405 say we definitely we want this to be public right. 29 00:01:25,405 --> 00:01:27,875 We want this to be accessible, that's the point of what we're doing. 30 00:01:27,875 --> 00:01:30,567 So we'll start with public and now, 31 00:01:30,567 --> 00:01:35,237 we need to define what type of data will be returned from this method, 32 00:01:35,237 --> 00:01:39,287 meaning when I call this method, what type of answer will I get back from it. 33 00:01:39,287 --> 00:01:40,807 For instance, let's look at one. 34 00:01:40,807 --> 00:01:43,437 So if we look at the method string.contains, 35 00:01:43,437 --> 00:01:45,557 remember you use that to find out if something's in there. 36 00:01:45,557 --> 00:01:48,157 So that is definitely a public method, right. 37 00:01:48,157 --> 00:01:52,690 We are using that, and it returns a Boolean okay. 38 00:01:52,690 --> 00:01:57,010 So we said that our method was going to be returning a string, right? 39 00:01:57,010 --> 00:01:58,080 It's the character name. 40 00:01:58,080 --> 00:02:02,150 It's a string so so we say, public string. 41 00:02:03,590 --> 00:02:08,085 And next is the method name so we're going to get the character names so 42 00:02:08,085 --> 00:02:10,720 getCharacterName. 43 00:02:10,720 --> 00:02:13,870 Now, here's the part that makes it different than the field definition. 44 00:02:13,870 --> 00:02:15,380 Look, it looks pretty similar, right? 45 00:02:15,380 --> 00:02:18,430 What we're going to do is we're going to open parentheses 46 00:02:18,430 --> 00:02:21,750 stating that this is indeed a method and it's expected to be called. 47 00:02:21,750 --> 00:02:24,390 So, here is where you would define parameters, 48 00:02:24,390 --> 00:02:25,810 that's if your method needs it. 49 00:02:25,810 --> 00:02:30,500 So, if we come back to the string contains method, so it's a public Boolean that 50 00:02:30,500 --> 00:02:35,420 contains is the name of the method and again it opens up its parentheses. 51 00:02:35,420 --> 00:02:41,750 And it needs to know, a parameter we pass in the string containing words right? 52 00:02:41,750 --> 00:02:42,520 Remember we did that. 53 00:02:42,520 --> 00:02:46,340 So back at our example of contains it needs you to specify what you're looking 54 00:02:46,340 --> 00:02:50,690 for the contains method takes a string argument. 55 00:02:50,690 --> 00:02:56,250 So it's a string of the matching text that you're looking for, right? 56 00:02:56,250 --> 00:02:58,525 It specifies that parameter list. 57 00:02:58,525 --> 00:03:01,900 We'll go over these in a bit but currently our method doesn't need any parameters. 58 00:03:01,900 --> 00:03:07,490 So we'll just go ahead and close it and then we'll start our methods body. 59 00:03:07,490 --> 00:03:14,210 So it's opening up a new scope and I'll write right away to put the closing brace. 60 00:03:14,210 --> 00:03:17,420 So you see all of the editor lines up the braces, it's really nice this way. 61 00:03:17,420 --> 00:03:22,370 So now if I come here, I know this is the class scope and here is this method scope. 62 00:03:22,370 --> 00:03:26,020 It's a nice way to make sure that everything's lining up in one class. 63 00:03:26,020 --> 00:03:28,330 Okay, back to our method. 64 00:03:28,330 --> 00:03:33,730 In this scope here in this code block we can write whatever we want. 65 00:03:33,730 --> 00:03:38,590 In here we can write whatever code the method should perform when it's called. 66 00:03:38,590 --> 00:03:39,890 Finally, what should happen is, 67 00:03:39,890 --> 00:03:44,660 it should return a value of its expected type, which is string. 68 00:03:44,660 --> 00:03:49,710 So it's going to use the keyword return. 69 00:03:49,710 --> 00:03:53,061 And we're going to return the characterName, right? 70 00:03:53,061 --> 00:03:57,700 And the character name is a string characterName. 71 00:03:57,700 --> 00:03:59,060 So here's the characterName. 72 00:03:59,060 --> 00:04:03,120 It's going to return this and because it's in the scope right it's in this class 73 00:04:03,120 --> 00:04:07,030 scope it has access to this even though it's private. 74 00:04:07,030 --> 00:04:12,990 The prefix of get here is part of a very common pattern. 75 00:04:12,990 --> 00:04:17,190 It's called a getter, this pattern of getter for a specific field 76 00:04:17,190 --> 00:04:22,365 is to name them with get followed by the name of the field just like we did. 77 00:04:22,365 --> 00:04:27,370 GetCharacterName so you can see it's get and 78 00:04:27,370 --> 00:04:31,530 then character a name and it's getting this private field character name here. 79 00:04:31,530 --> 00:04:34,710 So methods that are used to change the state of these objects or 80 00:04:34,710 --> 00:04:37,760 set a field value are prefixed with set and 81 00:04:37,760 --> 00:04:41,910 those are called of course setters so together these getters and 82 00:04:41,910 --> 00:04:46,100 setters are often referred to as properties in other languages. 83 00:04:46,100 --> 00:04:49,430 They'll become very common practice for you in your job adventures. 84 00:04:49,430 --> 00:04:52,220 Okay, so now let's flip back to the example. 85 00:04:52,220 --> 00:04:56,779 I'm gonna go ahead and before we do that, I'm gonna get rid of this comment here. 86 00:04:56,779 --> 00:05:01,420 I wanna save this file, and let's flip back to example and take a look. 87 00:05:01,420 --> 00:05:04,770 So we know that the code in here doesn't compile remember it wasn't compiling, 88 00:05:04,770 --> 00:05:05,310 so let's go ahead. 89 00:05:05,310 --> 00:05:08,180 Let's get rid of this Darth Vader cuz we're not gonna change that anymore, 90 00:05:08,180 --> 00:05:08,680 it's private. 91 00:05:10,650 --> 00:05:16,040 But now, instead of accessing the private field character name, let's try to access 92 00:05:16,040 --> 00:05:20,320 the public method, getCharacterName, and we're going to call that method. 93 00:05:20,320 --> 00:05:24,610 So there's some parens I'll put this down in a new line in here. 94 00:05:24,610 --> 00:05:27,620 This is highlighting, this is this, okay. 95 00:05:27,620 --> 00:05:30,756 So we're going to call the method, getCharacterName and 96 00:05:30,756 --> 00:05:32,742 that's going to return the method. 97 00:05:32,742 --> 00:05:34,960 So let's go ahead and let's compile it and run it. 98 00:05:37,000 --> 00:05:39,510 I forgot to save it important note there see the little red dot. 99 00:05:41,550 --> 00:05:42,560 So compile it and run it. 100 00:05:44,280 --> 00:05:46,060 Boom we got it, it's back. 101 00:05:46,060 --> 00:05:51,670 So again the method was called and our code inside of that method. 102 00:05:51,670 --> 00:05:57,305 This method body here was run and we returned the private field characterName. 103 00:05:58,430 --> 00:06:02,140 And that's definitely more like how things work in real life, right? 104 00:06:02,140 --> 00:06:05,610 People can't just go changing the character on these pez dispensers and 105 00:06:05,610 --> 00:06:09,680 now we are clearly communicating that reality because we haven't added a setter 106 00:06:09,680 --> 00:06:13,200 we've only add it together so nobody can set that. 107 00:06:13,200 --> 00:06:18,180 Now, one thing you're probably wondering is well, we can't change it at all, 108 00:06:18,180 --> 00:06:21,470 right, it's always going to be Yoda. 109 00:06:21,470 --> 00:06:22,260 That's a good point. 110 00:06:22,260 --> 00:06:25,780 Let's tackle that next awesome job modeling the character. 111 00:06:25,780 --> 00:06:28,530 Now it can't be changed only retrieved. 112 00:06:28,530 --> 00:06:31,400 This is a very common pattern used to express how you'd 113 00:06:31,400 --> 00:06:33,530 like people to interact with your object. 114 00:06:33,530 --> 00:06:35,910 We'll run through it a couple more times in the course just to make sure 115 00:06:35,910 --> 00:06:37,320 that it's sinking it. 116 00:06:37,320 --> 00:06:40,290 So we left another problem in there, didn't we? 117 00:06:40,290 --> 00:06:43,850 Every single Pez dispenser that we create is gonna be a Yoda character. 118 00:06:43,850 --> 00:06:46,980 That's certainly gonna bum out some kids who want a Teenage Mutant Ninja Turtle or 119 00:06:46,980 --> 00:06:50,630 an Elsa version, and for sure sales are gonna plummet. 120 00:06:50,630 --> 00:06:53,770 Let's do a quick exercise to make sure that we got this private keyword and 121 00:06:53,770 --> 00:06:55,060 getter method thing figured out, and 122 00:06:55,060 --> 00:06:57,710 then we'll fix this Yoda only situation that we got ourselves into.