 Hey everybody, it's Brian. Welcome to the 15th Flutter Tutorial. We're going to continue on our learning curve of Dart. We're actually getting towards the end of this here. If we go ahead and jump into language and then do a tour of Dart, you can see that we've covered pretty much all of this. Today we're going to backtrack a little bit and go back into classes a little bit here. I want to discuss static, my brain just shut down, static methods and static variables inside of the classes and how they actually interact with Dart. So let's just go ahead and crack this open. We're just going to call it stat, like that word stat. So if you are from another language, you're probably rolling your eyes going, I already know how this works. There are some minor caveats. If you're brand new programming, this may completely blow your mind. And I apologize in advance. So we're going to just kill that, kill that. And the first thing we're going to do is we're going to make a new class here. And we're going to call it counter. Now in the counter class, we're going to make a private variable here called static and let's call it count. So what does a static mean right here? Static is a keyword. You can tell it's a keyword because it's highlighted in bold. Really, what it means is it exists throughout every instance and non instance of that class. And we'll really get into that here in a second, but just understand that it means it always exists. So what we're going to do is we're going to say int, get, we're going to make a getter and setter for this. I swear my phone is dying. It just made the weirdest noise. Now, why am I making a getter and setter? We really don't have to. I'm just doing this to show you that, you know, you can have this as a private member variable, but also have it static. Some people get a little confused and think that just because it's static, it has to be public and it really doesn't. So now we're going to make some static methods here. So we're just going to increment and decrement respectively. And then we're going to make some non static methods here. And actually, we can just shorten these up as well. I don't know why I was going to do it this way, but so we can add, remove 10 respectively. And then we are going to just print the count out. Now you may be looking at this and asking some questions. What is static? Why is it all scattered throughout this? And how come we're using count and not the getter and setter? Remember, getter and setter are primarily used for outside of your class. Well, you're in your class, you have the inner scope, and therefore you can access this internally. Everything outside of this, because this is a private variable, will not be able to access it. So you have to use the getter and setter. And now we're going to go over to our main file here. We're going to endpoint the oops. That was a major fail right there. That was kind of embarrassing. So first thing we want to do here, let's just say counter. And you notice how if we just type the class name counter, we have the decrease increase in print count. And if we look at the class again, you can see that increase, decrease, and print count all have static. That's what it means is we don't have an instance of this class, yet we have those functions, or I should say those methods. And we can actually work with those. So we don't even have an instance of this, but yet still it works. We've gone from zero to one. And we can actually increase this a few times if we want. Now, one thing you should notice is we don't have access to the non static fields. And that is simply because they're not static, meaning you have to have an instance of that object. All right, so we're going to say counter, obj, just short for object. You notice how now we have access to the non static fields, but we do not have access to the static fields. So we have access to add 10, but we don't have access to increase or decrease. Boy screwed that one up. And this is why we're using our getters and setters so we can access those. And we're going to dereference that object. And once again, we're going to print the count. So what we're going to do here is we're accessing the static fields. And then we're going to make an instance of that object and modify it with an instance of that class, instance of that object would then make any sense, we're going to make an instance of that class, mean we're making an object. And then we're going to modify it using the non static fields. And then we're going to actually print the count out again, and we can see how the count actually increments. So we've got zero to because we've gone from zero plus one plus one. So there's our two. And then we have gone to 12 because we've added 10. And then we've set it to five. So there's our five. And then we've set it to null. And we set our object to null. But yet we can still print out five. And if we made another instance of this class, let's call this OBJ two. So we know it's a completely different object. Let's get rid of that. Right there. You can see that it stays at 15 because now we've added 10 to the five. And even though we've set this to null, it still stays there. So that in a nutshell is static fields. Now, one thing you should know is you don't always need static. A lot of people think I need to convert this to static and that to static. You really don't. Many times what you would actually need to do is persist this off to a storage device somewhere rather than actually holding it in memory. For a counter, it would actually make sense. Like let's say you're we're building a like a humane society that houses animals and you want to know how many animals are in there. Every time an animal is added or removed, you would increase the counter. But for normal lists and things like that, static really does not make a whole lot of sense. What is a good use case for static? Well, this right here actually, if you want to make a what's called a utility class or class that has a lot of functions in it or methods in it that you would just use throughout the day, rather than having to actually build an instance of that object, you can just say class dot whatever it is, and then you've got all your built in functionality. There's actually a lot of libraries that do that. So that's a good use case. That's all for this tutorial. I hope you found this educational and entertaining the source code for this and all other tutorials will be out on voidroms.com. And you just click on GitHub and it takes you right out to the tutorials. And there is a Voidroms Facebook group. The link is out here in the contact tab. And we've got 1700 plus and counting other programmers out there from all different languages that can help out.