Static vs Instance Methods6:24 with Jeremy McLain
Methods that are called directly on a class name are static methods. Don't confuse them with instance methods which are called on unique instances of the class (AKA objects).
Towers can only shoot so far. 0:00 So we need to know if an invader is in range of a tower. 0:02 For that we need to calculate the distance between two points on the map. 0:06 We should write a method to perform this calculation and 0:11 return the distance between two points. 0:13 First we need to decide where to put this method. 0:16 Take a moment to think about which of these classes this method should go in. 0:20 We should think about which class has the most in common with this method. 0:25 This method will only deal with points. 0:29 So the natural place to put it is the point class. 0:32 That way we can ask any point how far away it is from any other point. 0:35 This method will need to be public. 0:41 Since we're only working in whole units it will return an integer. 0:43 We'll call our method DistanceTo. 0:47 And it will take x and y parameters. 0:49 So this method will need to calculate the distance between the coordinates passed 0:54 in and those inside of the point object. 0:59 For that we can use the Cartesian distance formula, 1:02 which tells us exactly how far two points are from each other. 1:05 I'm having a little trouble remembering how to do the Cartesian Distance Formula. 1:10 Let's search for it using Google. 1:15 So we'll go to Google and type in Cartesian Distance Formula. 1:18 Here's a link that looks promising. 1:29 Now I remember, 1:33 it's the square root of the sum of the squared differences of each component. 1:35 Let's code up this formula by working from the inside out. 1:40 The innermost operation involves subtracting 1:44 the x components from each other. 1:47 Let's do that and assign the results to variables. 1:50 So we'll say int xDiff. 1:52 Difference is a long word so I'll just shorten it to Diff. 1:57 Equals X- x and I’ll do the same thing with y. 2:00 So this is subtracting the x and y that was passed into this method from the x and 2:16 y that are in the point that this method was called on. 2:21 Let's look at the formula again. 2:27 We need to square the differences. 2:30 Squaring a value is just multiplying it by itself. 2:34 We can do that for both of these pieces and then assign the results to a variable. 2:38 Let's call it xDiffSquared. 2:42 XDiffSquared will be xDiff * xDiff. 2:52 Will do the same thing to get yDiffSquared. 3:01 Now the formula says that we need to add them together and take the square root. 3:13 How do we take the square root of a value in C#? 3:19 That sounds like a good question for Google. 3:22 Let's go to Google and type in C# square root. 3:26 The top result is from the Microsoft Developer Network. 3:33 And says, return the square root of a specified number. 3:36 That looks like what we want, let's click on the link. 3:39 Looking at this we can tell that it takes a parameter of type double and 3:43 returns a double. 3:49 We can also see that it's in the Math class which is in the System namespace. 3:51 If you take a closer look at this method definition here, you'll notice this static 3:56 keyword between the public access modifier and the return type. 4:00 Math.Square root is what's known as a static method. 4:04 Static methods are different than the methods we've been creating. 4:09 They're different because they're called directly on the class. 4:13 We don't need to construct an object before we can use them. 4:17 This starts to make sense when we look at how they're called. 4:21 Let's go down here and look at an example of how it's used. 4:24 As you can see, they typed Math.Sqrt. 4:29 Math, with a capital m, is the name of the Math class. 4:33 There's no Math object being used. 4:38 The square root method is being called directly on the class. 4:40 This is what makes static methods special. 4:45 We've actually already called a static method in our code. 4:48 Take a look at the Game.cs file. 4:52 We called a static method when we called WriteLine here. 4:56 Console is the name of the class. 5:01 And we're calling WriteLine directly on the Console class 5:04 without constructing a console object. 5:08 Contrast that to the way we use the OnMap method. 5:11 The OnMap method is not static. 5:17 So we had to construct a map object here. 5:20 And then call OnMap. 5:24 In fact, the Main method, which is the first method called in the program, 5:28 has to be a static method. 5:32 That's why it has the static keyword here. 5:36 This allows the Main method to be called without first creating a game object. 5:40 The OnMap method and the DistanceTo methods are known as instance methods, 5:46 because they're called on objects. 5:51 We learned earlier that objects are also known as instances of a class. 5:54 Math.Sqrt and Game.Main are static methods. 5:59 They're called directly using their class name. 6:05 So whenever we see a method being called directly on the name of a class 6:08 like Console.WriteLine is, we know that the method is static. 6:12 Let's take a quick break. 6:18 When we come back, we'll go back to the DistanceTo formula and 6:19 finish coding it up. 6:22
You need to sign up for Treehouse in order to download course files.Sign up