instantiating our Class
At this point we know how to build our own classes; Just as when we worked with the Object/Array complex data types we started them up with 'new DataType' to create a new instance, we will do the same for our object. We will peak at our constructor method and see what its role is in the process.
Video Transcript
00:00Now, one thing I want to know before we start working is I made a little typo, and if we
00:05just jump into folders at this point, I made a small type of word.
00:08My folder itself is called time, but in my class definition, my package, I set my package
00:13to be timer.
00:14Now, I'm not going to fix this error just because it's a good practice where you're
00:19going to encounter that, and just now that we know it, as we're going to build the application,
00:24we're going to be prepared for that scary moment where we're going to get a weird
00:27error as we try to run our application, okay?
00:31So what I want to do next is I just want to, let's get our timer out, our timeout into
00:36the world.
00:37Let's build it out at this point.
00:38So I'm going to go into my app class, and in my app class, I just want to create quickly
00:43as we did so far inside of my constructor, I'm going to create a new variable, and again,
00:48I just want to recap because it's an important point to note that just as when we created
00:53a new object, all we really had to do is set a new object and open, close a round bracket.
00:59We could have also done the shortcut, right?
01:01The curly bracket, open, close, and that would create a new object as well.
01:05Or we could have created a new array, which we've done before as well, which would do
01:09array, and we had also a shortcut for this baby.
01:13But as we're building our own classes, we're working with not every single class as a
01:18shortcut, obviously, but behind the scenes, even though we typed, if we typed only open
01:22close curly bracket, or if we typed only our round brackets, behind the scenes, what Flash
01:27was doing was a new, and the data type, which was the class, okay?
01:33And that's why it's also needing, you know, when you see the open close bracket, it kind
01:36of like the round ones, it kind of reminds you of one of the function brackets.
01:41And why is that is because when it goes, when it's creating a new array, it's not
01:45only approaching the class name itself, it's approaching that constructor function.
01:50And starting up the class with that constructor function.
01:54So if I had any information inside of this constructor, it would automatically build those
01:58custom specific behaviors that we want to set for every unique object that we create.
02:04We're going to see what that means in just a second.
02:06So let's, before we even do that, let's try to build out our application.
02:10So to do that, I'm just going to set a new to, I'm going to call it to for time out.
02:14And I'm going to set it as a timeout.
02:16Now before I do that, I'm going to import it, even though we could do it automatically.
02:21It's also good for our practice, I'm going to go up into my import, it knows if you're
02:24working in the older version of flash, so it won't do it automatically.
02:28And I'm going to type com dot everything fla dot my timer package.
02:34And again, I have an error because it's not really time rates, it's supposed to
02:37be time.
02:39And last thing I want to actually import my timeout object itself, or my class itself,
02:44which I'm going to create into an object in a second.
02:47What does that mean when I'm saying I'm importing it?
02:49When I'm importing it, it's not really pulling those files and adding it into your
02:53file size yet.
02:54What it's actually doing is it's basically telling to flash, this is the file that
03:00I mean, if I'm going to call out the name timeout inside of my application, all right,
03:05inside of it, more accurate inside of my class, because every separate class we have
03:09our import separately.
03:11Now, why that is important, we talked about that we might have different files
03:15that have the exact same name.
03:16But if I just call timeout directly inside of my application, flash wouldn't
03:20know what that is, because there's no timeout in our root, it's not in the
03:23package.
03:24So we have to tell flash that we wanted to import those behaviors from that
03:28specific folder.
03:29Now, yeah, if you're working in flex, or you're working in CS5, these stuff
03:34happened kind of like automatically so quickly that it's not that noticeable.
03:37But it's a very important factor to notice, because then if you have an issue,
03:41if you didn't mean this timeout, like, for example, you might have a library
03:44of animations that you're using, and then you download a newer version.
03:49But by mistake, automatically, because it's inside both of them are in your
03:52library for some reason, you're importing the older version, it's not
03:56doing what you expected it to do.
03:58Okay, so that's a legitimate scenario, right, okay, it's importing it,
04:04it's in a different package, you know, because the creator of that package
04:07changed the packaging name at some point.
04:10I'm going to set my data type to my timeout, and I'm going to set a new
04:15create a new timeout, open and close a round bracket, and I should be good
04:21and set to go.
04:22So if I run my application right now, besides the error that we're
04:25expecting, our application should run.
04:27Now, this error looks really, really scary, and it doesn't look like what we
04:31really would hope it would look like, we're getting so many errors.
04:34But it's kind of hinting to us that all these errors are basically the
04:37same error.
04:38It's telling us that it can't find, it can't find, it doesn't know, it's
04:40undefined, couldn't find, seems like that is something about that not
04:44compile time couldn't find, it sounds like that there's something
04:47missing.
04:48Now, the best way to debug something like that when you see a lot of
04:50errors, is first trying to go to the first error, so if we double click
04:54on it, it's going to tell us as soon as we start to create our object,
04:57it's already a problem.
04:58And that's already hinting to us that we're having a problem, it might
05:01be the name of the class, it might be the package of it, it might be
05:06that somewhere we're not writing it correctly.
05:09And if we would go back into our folder, we'd say, oh, okay, we're
05:12our timer, and it might take you a few moments, but knowing that will
05:16help you.
05:17So, first of all, we want to import time instead, and we want to
05:20change it up here also to time as well, because our package is
05:23time.
05:24Our folder structure is fine, because we set our folder structure
05:27correctly.
05:28But just to make sure, running in CS5 will automatically will
05:31update and we can see the package, our package, and we could
05:34select our class.
05:35Now if I run my application just one more time, we should get rid
05:39of those errors and we should be in a good shape at this point.
