Static Classes5:11 with Jeremy McLain
Static classes can't be instantiated or inherited, but they have their purpose.
In C# not everything has to be an object. 0:00 We often have a need to create a set of methods and properties that can 0:03 be accessed from any class without having an instance of a class to work with. 0:06 This is what static classes are for. 0:10 Static classes can't inherit from any other class. 0:13 Nor can static classes be inherited from. 0:16 We also can't instantiate a static class. 0:18 Instead of working with objects of the class, 0:21 we work directly with the class itself. 0:24 An example of a static class in the .NET framework is the System.Math class. 0:26 System.Math is simply a group of math functions that can be called. 0:31 We call them directly on the Math class itself. 0:36 We don't need to have a math object to use them. 0:39 Another example of a static class that we're familiar with 0:41 is the system.console class. 0:44 It makes sense for the system.console class to be a static class because 0:46 a program can interact with only a single console window. 0:50 You may have noticed that we can call methods directly on the console class, 0:53 without first creating a console object. 0:57 Members in a static class are globally accessible inside the application. 1:00 Public fields and 1:04 properties inside of a static class are known as global variables. 1:05 Typically, we want to avoid creating and using global variables because 1:10 we have very little control over who can change them. 1:14 This can cause bugs that are very difficult to track down and resolve. 1:17 And this is something we want to be aware of when creating static classes. 1:20 It's best to create static classes where the members of the class are immutable. 1:24 An immutable class is a class where the data inside the class can't be changed. 1:28 System.Math is an example of an immutable class. 1:32 System.Math is simply a collection of related functions. 1:36 There are some cases where it is all right to have a mutable static class 1:40 if the side effects of changing the data in the class is manageable. 1:45 System.console is one example. 1:48 We can use a static class to simplify some of the code in the Treehouse Defense game. 1:51 Let's take a look at the Tower class. 1:56 The Tower class contains a static field named _random. 2:00 We also have a static random field in shielded invader. 2:05 We can refactor this to use a single static random class. 2:08 We'll create our own static random class in random.cs 2:13 We'll put it in the TreehouseDefense namespace. 2:21 This is a good example of why we need namespaces. 2:25 We already have a class named random in the system namespace. 2:28 Because these classes are in different namespaces, 2:32 we can specify precisely which one we want to use. 2:34 Inside the class, we'll have a static random field and 2:38 initialize it to an instance of the Random class. 2:41 So, it's a private static 2:44 Random_ random = new Random. 2:49 We need to specify that we want to use System.Random here, 2:55 not the random class that we're in. 2:58 The method in the random class that both the Tower class and 3:02 the shielded invader class use is next double. 3:05 Remember the next double method when called, 3:08 returns a random double between zero and one. 3:11 We'll create our own static next double method that returns the result of 3:14 calling the random objects next double method. 3:17 Essentially what we've done here is created a global random object. 3:24 Now whenever random.next double is called, 3:28 it will get the next random number from the global random object we have here. 3:31 Now we can go back to all the places that is using System.Random. 3:36 And replace them to use our global random class. 3:40 So, here in the Tower class, we'll delete this line and 3:44 change this to our new static Random class. 3:47 We can do the same in shielded invader. 3:54 Delete this line and change this, so that it refers to the random class. 3:59 Let's compile to make sure we didn't break anything. 4:06 I got a compiler error here. 4:11 It says, cannot declare instance members in a static class. 4:13 Hm, that's because we didn't make the next double a static method. 4:17 Try it again. 4:25 There we go, so why would we wanna use our static class 4:27 instead of the way we were doing it before? 4:30 The way we were doing it before here in Tower and shielded invader is fine. 4:32 But by creating a static random class, 4:36 we've been able to slightly simplify the code in Tower and shielded invader. 4:38 Whenever we want a random number between zero and one, we can just call 4:43 random.nextdouble instead of first creating an instance of the random class. 4:46 Our random class is not immutable because the state of the random object here is 4:51 changed every time we ask for a new random number. 4:56 That's how it knows to give us the next random number. 4:59 But this is all right, since all we care about is getting a random number. 5:02 This is one of the rare cases where having a globally mutable object is okay. 5:06
You need to sign up for Treehouse in order to download course files.Sign up