Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Object Initialization5:14 with Jeremy McLain
We can set the values of an object's fields to an initial value when it's created.
A map without both width and height isn't very useful. 0:00 We can make sure that both of these fields are filled out when the object is created 0:04 by providing a special kind of method called a constructor method. 0:09 They're called constructors because they're used to construct 0:13 new instances of a class. 0:17 Let me show you what that looks like here. 0:20 Constructor methods are named the same as the class they're in. 0:22 The constructor is called when the object is created. 0:29 If we add parameters to the constructor method, then the user of this class 0:32 must provide those parameters in order for the object to be constructed. 0:37 We want the user to specify both the width and a height, so 0:41 we'll add parameters for width and height. 0:44 Now we need to use these parameters to initialize the fields. 0:48 We do that in the body of the method. 0:52 The Width and Height fields that were declared up here 1:02 are called instant variables because they exist as long as the object exists. 1:05 The variables width and height here, with all lowercase letters, 1:11 are called method level variables, because they only exist inside this method. 1:15 That's the variable scope. 1:21 We can only use these variables inside this method, but 1:24 these instance variables here can be used by other methods, and even other classes. 1:28 Again naming conventions determine how we name variables. 1:34 It's standard practice to name method parameters and 1:38 method level variables starting with the lower case letter. 1:41 Methods and public instance variables have an uppercase letter. 1:44 Check the teacher's notes for a list of naming conventions. 1:49 The primary purpose of a constructor is to initialize the object's 1:53 fields with some initial values. 1:57 One question we should ask ourselves when working with fields and constructors is, 1:59 should the values of the fields be able to change after the object is constructed? 2:05 In our case it doesn't make sense for 2:10 the size of the map to change after we've created it. 2:12 We can make sure that the user of this class 2:16 doesn't accidentally change the values by making these fields read only. 2:19 We do that by putting the read only keyword here. 2:24 So now a map must have both a width and a height. 2:35 And the width and the height cannot change after it's created. 2:39 We've accomplished this by adding a constructor and 2:43 using the read-only modifier. 2:47 You'll notice that constructors don't have return types. 2:49 They're only used to initialize the object. 2:53 And they don't return anything. 2:55 Usually if a method doesn't return anything we would type void here 2:57 just to the left of the method name. 3:02 Remember the void keyword means that the method doesn't return anything. 3:05 We don't do that on constructor methods though, 3:09 because constructors can't return anything. 3:12 We do need to do one more thing before we can use our constructor. 3:16 We need to make it public, just like we did with the width and height fields. 3:20 This allows other classes to construct a map using this constructor. 3:25 Let's go back to the game class now, 3:31 to see how to use this constructor to create an object. 3:33 Before we make any changes, let's try to compile the code as it is. 3:37 So I'll open up the Console window. 3:42 And type mcs 3:46 -out:TreehouseDefense.exe*.cs. 3:50 We can see how the compiler enforces the new rules that we've established for 4:01 the map class. 4:05 We're getting the first error because we didn't pass in two 4:07 arguments to the constructor. 4:10 We can add those arguments here between these parentheses 4:12 where we're creating a new map object. 4:15 Let's initialize our map with a width of 8 and a height of 5. 4:17 This resolves the first error. 4:22 The other two errors are happening because we tried to assign values to width and 4:24 height after the object was created. 4:29 This is no longer permissible because we made both of these fields read only. 4:32 We initialize these fields in the constructor instead. 4:37 So we can remove these two lines. 4:41 We can still read the values of width and height though. 4:43 So this line, where we calculate the area, is perfectly legal. 4:47 Let's compile again to make sure we're following our new rules. 4:51 Excellent. 4:58 If we had broken any rules, 4:59 we'd see compiler errors here instead of just this warning. 5:01 The map class is starting to define what it means to be a map. 5:06 We'll add even more to this class in the next video. 5:10
You need to sign up for Treehouse in order to download course files.Sign up