Methods6:58 with Craig Dennis
Let's create our first method, a getter.
Okay so we've fixed our problem with restricting changes to our character name, 0:00 but it's a little too restrictive. 0:05 It can't even be accessed from outside the class itself. 0:07 We want to allow consumers of our objects to still be able to see the value, 0:09 even if they can't change it. 0:13 So how do we let them get the value then, you ask? 0:15 Great question. 0:18 Well the answer is we build a method that exposes a value. 0:19 You've used methods before but for instance on the console object use 0:23 the print F method or on strings we use the two lower case method. 0:27 Now remember what I said that developers modeling the real world objects focus on 0:31 two main characteristics which are state and behavior. 0:35 Well fields usually are used to express that state and 0:38 its methods help us to express that behavior. 0:41 So, therefore you can kind of think of fields as characteristics about your 0:44 object and methods as the verbs or actions that your object can perform. 0:47 So, just like how you've use methods on objects before, like string contains or 0:51 integer percent, it's time that we learn how to write our own. 0:55 Okay, so let's do this. 0:59 Let's add a method to our object that behaves like this. 1:00 When the method is used or called it will respond with or 1:04 return to the calling code. 1:08 The value of the private field character name. 1:10 Now defining a method is pretty similar More to the way that we define a field. 1:14 They also have access modifiers. 1:18 So let's come here. 1:21 So when I come in here and 1:22 say we definitely we want this to be public right. 1:23 We want this to be accessible, that's the point of what we're doing. 1:25 So we'll start with public and now, 1:27 we need to define what type of data will be returned from this method, 1:30 meaning when I call this method, what type of answer will I get back from it. 1:35 For instance, let's look at one. 1:39 So if we look at the method string.contains, 1:40 remember you use that to find out if something's in there. 1:43 So that is definitely a public method, right. 1:45 We are using that, and it returns a Boolean okay. 1:48 So we said that our method was going to be returning a string, right? 1:52 It's the character name. 1:57 It's a string so so we say, public string. 1:58 And next is the method name so we're going to get the character names so 2:03 getCharacterName. 2:08 Now, here's the part that makes it different than the field definition. 2:10 Look, it looks pretty similar, right? 2:13 What we're going to do is we're going to open parentheses 2:15 stating that this is indeed a method and it's expected to be called. 2:18 So, here is where you would define parameters, 2:21 that's if your method needs it. 2:24 So, if we come back to the string contains method, so it's a public Boolean that 2:25 contains is the name of the method and again it opens up its parentheses. 2:30 And it needs to know, a parameter we pass in the string containing words right? 2:35 Remember we did that. 2:41 So back at our example of contains it needs you to specify what you're looking 2:42 for the contains method takes a string argument. 2:46 So it's a string of the matching text that you're looking for, right? 2:50 It specifies that parameter list. 2:56 We'll go over these in a bit but currently our method doesn't need any parameters. 2:58 So we'll just go ahead and close it and then we'll start our methods body. 3:01 So it's opening up a new scope and I'll write right away to put the closing brace. 3:07 So you see all of the editor lines up the braces, it's really nice this way. 3:14 So now if I come here, I know this is the class scope and here is this method scope. 3:17 It's a nice way to make sure that everything's lining up in one class. 3:22 Okay, back to our method. 3:26 In this scope here in this code block we can write whatever we want. 3:28 In here we can write whatever code the method should perform when it's called. 3:33 Finally, what should happen is, 3:38 it should return a value of its expected type, which is string. 3:39 So it's going to use the keyword return. 3:44 And we're going to return the characterName, right? 3:49 And the character name is a string characterName. 3:53 So here's the characterName. 3:57 It's going to return this and because it's in the scope right it's in this class 3:59 scope it has access to this even though it's private. 4:03 The prefix of get here is part of a very common pattern. 4:07 It's called a getter, this pattern of getter for a specific field 4:12 is to name them with get followed by the name of the field just like we did. 4:17 GetCharacterName so you can see it's get and 4:22 then character a name and it's getting this private field character name here. 4:27 So methods that are used to change the state of these objects or 4:31 set a field value are prefixed with set and 4:34 those are called of course setters so together these getters and 4:37 setters are often referred to as properties in other languages. 4:41 They'll become very common practice for you in your job adventures. 4:46 Okay, so now let's flip back to the example. 4:49 I'm gonna go ahead and before we do that, I'm gonna get rid of this comment here. 4:52 I wanna save this file, and let's flip back to example and take a look. 4:56 So we know that the code in here doesn't compile remember it wasn't compiling, 5:01 so let's go ahead. 5:04 Let's get rid of this Darth Vader cuz we're not gonna change that anymore, 5:05 it's private. 5:08 But now, instead of accessing the private field character name, let's try to access 5:10 the public method, getCharacterName, and we're going to call that method. 5:16 So there's some parens I'll put this down in a new line in here. 5:20 This is highlighting, this is this, okay. 5:24 So we're going to call the method, getCharacterName and 5:27 that's going to return the method. 5:30 So let's go ahead and let's compile it and run it. 5:32 I forgot to save it important note there see the little red dot. 5:37 So compile it and run it. 5:41 Boom we got it, it's back. 5:44 So again the method was called and our code inside of that method. 5:46 This method body here was run and we returned the private field characterName. 5:51 And that's definitely more like how things work in real life, right? 5:58 People can't just go changing the character on these pez dispensers and 6:02 now we are clearly communicating that reality because we haven't added a setter 6:05 we've only add it together so nobody can set that. 6:09 Now, one thing you're probably wondering is well, we can't change it at all, 6:13 right, it's always going to be Yoda. 6:18 That's a good point. 6:21 Let's tackle that next awesome job modeling the character. 6:22 Now it can't be changed only retrieved. 6:25 This is a very common pattern used to express how you'd 6:28 like people to interact with your object. 6:31 We'll run through it a couple more times in the course just to make sure 6:33 that it's sinking it. 6:35 So we left another problem in there, didn't we? 6:37 Every single Pez dispenser that we create is gonna be a Yoda character. 6:40 That's certainly gonna bum out some kids who want a Teenage Mutant Ninja Turtle or 6:43 an Elsa version, and for sure sales are gonna plummet. 6:46 Let's do a quick exercise to make sure that we got this private keyword and 6:50 getter method thing figured out, and 6:53 then we'll fix this Yoda only situation that we got ourselves into. 6:55
You need to sign up for Treehouse in order to download course files.Sign up