 Hey everybody this is Brian and welcome to the eighth tutorial on Flutter and Dart. We're going to go ahead and continue our conversation on classes. And let's actually call this class two. Well let's call it classes two. There we go. And while that's churning away we're going to look at what we're going to cover today is called constructors. What are constructors? Well whenever you have a class and you create a new instance of it something has to happen in the background and that's what we're going to cover today. That background is called a constructor. It's when the object is actually constructed in memory. And to kind of solidify that main point here what we're going to do is I'm going to go out here and we're going to say new house.dart. And I'm kind of doing this unscripted meaning I do not have my notes I'm just kind of typing so there probably be some errors and maybe some goof ups. And we're going to keep this class very simple because we're not focused on the class itself but rather the constructor. So width equals zero and length. All right so we've got that and we need to import house.dart. And we're going to say house, house one equal new house. So we've done this before we know what this is it's just going to make a new house. What's going on under the hood here is it's looking at your blueprint and saying what do I need to do. And then it's going out and making a new instance of this. And hold on let me turn my music down. I can't even hear myself think here. I have to keep music on the background or my headset shuts off it's kind of like an annoying feature but uh anyways uh so it's saying what do I need to do here. Now it knows what it needs to do by way of what's called a constructor and every class has what's called a default constructor meaning it just magically exists. And if you're wondering where it is it's right here boom. So somewhere in there under the hood on node u is a function that looks like this. And it probably just says return would be my guess. Now all that is it's called the default constructor but it doesn't exist in a manner that you can see it it's under the hood and you'll never access it. It exists solely for the compiler. Now if you want to actually play around with it you can add it in constructed. So what happens now is when we run this you'll see it suddenly says boom constructed where's that getting called. We haven't changed anything in our main. So it's saying all right here's the blueprint we want to use. Here's the name of the variable we want to use. Make a new instance of that and use this constructor. In this case it's saying use the default constructor. You can tell it's a default because there is a you know parentheses with nothing in it that's called the default constructor. To solidify that we're going to make yet another one and it's going to say default constructor already defined. Meaning it knows that's the default because it's just an empty set of parentheses. Okay that's great. What do you need a constructor for? What's the whole purpose of this thing? Constructor says hey what if you want to initialize a set of variables before you actually use them. In this case let's say we want to use the width and the length. We don't want those to be zero. So whenever a new house is created we want to automatically set those variables and we want to force whoever's using this class to set those variables. So we're going to say int w, int l and we're going to say width equal w, length equal l and we're going to print these out. Fat fingered that one. All right so when we go to run this now you guessed it it's going to crash. Bang. Why? No constructor house with matching arguments declared in class home. Whenever you see that your first thing should be look for the new keyword and then you'll see the error right next to it. It says two required arguments but zero found. Well how do we know what those arguments are? By looking at the constructor we have two integers we have to deal with here. So let's say we want it to be five by nine. Bang. So now suddenly we have a width of five and a length of nine. We can actually take that a step further and make this you guessed it optional. And let's say this is five. Let's say this is nine. Let's go back to our program here and notice how we've got to actually name them out now. I'm doing this for illustrative purposes and we want to say we want the width to actually be six instead of five and it should be six by nine. There we go. So that in a nutshell is how to deal with a constructor and why you need one. Now special note. Dart does not have, at least I haven't found, the concept of a deconstructor and what a deconstructor is is when the class is actually being destroyed. Under the hood there's this thing called garbage collection and garbage collection is a very complex topic but it's very easy to understand. When you are creating variables right here the use of the new keyword you are putting that out on memory and you're putting it on something called the heap. Under the hood it may actually be somewhere else but the heap is just a giant free store memory. Think of it like a giant warehouse. You're basically opening the door of the warehouse and throwing a house in it and saying here store this somewhere I don't care where. The problem is when your program dies or she gets shut off or the user closes it or whatever that house still sits out memory so you need to actually delete that. Garbage collection takes care of all that mess for you. All you have to do is say I want a new house and I want you to put it somewhere I don't care where and let me access it when I need it and then when your program is closed or if that variable goes out of scope let's say we have a function up here and let's move this up here whoops and let's call test does the same thing however something drastically different is happening in the background here. What we're going to say and I'm just going to put in comments here and then flag for garbage collection that's what's really going on in the background here so when this test function is called it's going to go up and it's going to say create a new house bang then it's going to call our constructor whatever the constructor may be then in the background it goes out of scope. What happens is everything that was created in the scope is now flagged for garbage collection. What that means is it is no longer being used by the program and the program thinks it's safe to delete it out of memory. Now if you've ever worked with older languages like c and c++ if you try to access something that doesn't actually exist in memory your whole program just goes kaboom and crashes. Garbage collection takes all that headache away meaning you don't have to worry about it you just have to create it it'll take care of it and then when you no longer using it anymore it'll get rid of it for you if your program crashes and dies an ugly death it'll delete all that memory for you you don't have to worry about it so why does all this matter to you because all of this is being done under the hood now there's a special case what if and just follow me here you want to actually do something with this and then close it out you really should and there's probably a million other ways to do this dispose your class meaning and this is very similar to actually built in I think in some of the languages kill me now you would call that before the end here so you would say you may be tempted to do something like that now why would you do this let's say you open a file or database or network connection you would want to actually close it here so you would say close all open connections or close all the things that would be a good way to do it but remember under the hood garbage collection is being called so if you forget to do any of this it's going to happen automatically and you don't have to worry about it that's a very nice concept there are more advanced topics to it but that in a nutshell is constructors and deconstructors I hope you found this educational entertaining and thank you for watching if you're so inclined you can get the source code for this and other tutorials out on my website just go out tutorials tutorials click on the tutorials link and go out to get hub and I will have all the code out there thank you