1 00:00:00,850 --> 00:00:04,590 Objects and software allow us to express and model things that we have and 2 00:00:04,590 --> 00:00:06,040 use in real life. 3 00:00:06,040 --> 00:00:08,650 Programmers have discovered that all real world 4 00:00:08,650 --> 00:00:13,190 objects share 2 important characteristics they have state and behavior. 5 00:00:13,190 --> 00:00:16,200 By creating an object in code that maintains its own state and 6 00:00:16,200 --> 00:00:18,110 presents its behavior for usage. 7 00:00:18,110 --> 00:00:20,760 It allows you to hide how things are actually working from 8 00:00:20,760 --> 00:00:21,589 users of your object. 9 00:00:22,880 --> 00:00:27,333 A great example of this is a radio that has some state is it on or 10 00:00:27,333 --> 00:00:29,126 off what station is it? 11 00:00:29,126 --> 00:00:29,660 [MUSIC] And 12 00:00:29,660 --> 00:00:32,836 it also has some behavior that it exposes to the use for 13 00:00:32,836 --> 00:00:35,430 the power on change the station. 14 00:00:35,430 --> 00:00:36,840 How does it actually work? 15 00:00:36,840 --> 00:00:38,400 I have no idea. 16 00:00:38,400 --> 00:00:42,000 Its inner workings are hidden from me but its behavior is exposed and 17 00:00:42,000 --> 00:00:44,650 I can manipulate the state that allows me to change. 18 00:00:45,970 --> 00:00:49,180 Before we go any deeper I want to apologize to you for 19 00:00:49,180 --> 00:00:51,320 what I'm about to do to your brain. 20 00:00:51,320 --> 00:00:54,600 Chances are after I introduce this concept to you, 21 00:00:54,600 --> 00:00:58,230 you will not be able to stop thinking about how you would create 22 00:00:58,230 --> 00:01:01,980 everything you see in real life as an object in code. 23 00:01:01,980 --> 00:01:05,772 Like an ear worm when somebody sings, We Built This City. 24 00:01:05,772 --> 00:01:08,490 You're gonna be building an object representation 25 00:01:08,490 --> 00:01:11,200 of every single thing you see. 26 00:01:11,200 --> 00:01:12,030 Sorry about that. 27 00:01:12,030 --> 00:01:15,370 Okay, so let's explore a real life object together. 28 00:01:15,370 --> 00:01:17,710 How about this Yoda Pez dispenser right here. 29 00:01:17,710 --> 00:01:20,040 Now, if you haven't seen a Pez dispenser before, 30 00:01:20,040 --> 00:01:22,910 they're a little toy that serves candy when you flip back their head. 31 00:01:22,910 --> 00:01:24,780 They usually have different character heads on them and 32 00:01:24,780 --> 00:01:27,800 they're collector's items, like this one here from Star Wars, the hero, Yoda. 33 00:01:28,940 --> 00:01:31,390 So, let's see, it definitely has some state. 34 00:01:31,390 --> 00:01:32,160 Is it empty? 35 00:01:32,160 --> 00:01:32,660 Nope. 36 00:01:33,780 --> 00:01:34,490 How many are in there? 37 00:01:34,490 --> 00:01:36,220 It looks like there's about ten. 38 00:01:36,220 --> 00:01:38,140 There's also some behavior, right. 39 00:01:38,140 --> 00:01:40,560 Namely its job: dispense when we do this. 40 00:01:40,560 --> 00:01:42,910 It changes the state of the Pez dispenser. 41 00:01:42,910 --> 00:01:47,250 Now there are ten and now there's nine and it can also be loaded 42 00:01:47,250 --> 00:01:49,880 like I could add more Pez in here and would also change state. 43 00:01:51,790 --> 00:01:52,900 Come to think of it. 44 00:01:52,900 --> 00:01:55,030 All Pez dispensers kind of work the same. 45 00:01:55,030 --> 00:01:57,170 They just have different character heads. 46 00:01:57,170 --> 00:01:59,690 If we imagine the factory where these are made. 47 00:01:59,690 --> 00:02:02,410 I bet there is some sort of master blueprint of the Pez 48 00:02:02,410 --> 00:02:05,220 dispenser that is used to create each one of these. 49 00:02:05,220 --> 00:02:07,920 And then customizes the character for each one that comes through. 50 00:02:08,960 --> 00:02:13,230 This blueprint used to create objects in Java is called a class. 51 00:02:13,230 --> 00:02:15,580 Could we build something like this blueprint in code and 52 00:02:15,580 --> 00:02:16,960 then create objects from it? 53 00:02:16,960 --> 00:02:19,210 We definitely can and it's pretty straightforward. 54 00:02:19,210 --> 00:02:19,940 Let's do just that. 55 00:02:21,575 --> 00:02:24,445 Okay, so what we'll do is we'll create a new class and 56 00:02:24,445 --> 00:02:26,345 then use it in a console application. 57 00:02:26,345 --> 00:02:30,605 Now, I've gone ahead and I've built the console application boilerplate for 58 00:02:30,605 --> 00:02:33,185 us again it's in a file called Example.java, let's open that up. 59 00:02:35,190 --> 00:02:39,760 So you might have used the java.io console object before to take input and 60 00:02:39,760 --> 00:02:40,980 write out to the screen. 61 00:02:40,980 --> 00:02:44,450 I've had several students ask what the difference between the console and 62 00:02:44,450 --> 00:02:47,050 something that they've seen used elsewhere and that's system.out. 63 00:02:47,050 --> 00:02:51,130 Well, the answer is that console is actually just a convenient wrapper around 64 00:02:51,130 --> 00:02:52,870 system out and system in. 65 00:02:52,870 --> 00:02:56,580 We aren't going to be taking any input right now let's take a look at this common 66 00:02:56,580 --> 00:02:58,480 pattern for writing out to the console. 67 00:02:58,480 --> 00:03:02,450 So System is a class that's automatically imported, right? So System, and 68 00:03:02,450 --> 00:03:07,820 it provides a static public field named out. Out is a print stream and 69 00:03:07,820 --> 00:03:11,206 it exposes some methods that we've seen before on the console object. 70 00:03:11,206 --> 00:03:14,260 Now, don't worry [LAUGH] about that mouthful that I just said 71 00:03:14,260 --> 00:03:19,040 just know that we can write out to the screen using .println. 72 00:03:19,040 --> 00:03:23,610 So println means print line and what that means is it will add a new line at 73 00:03:23,610 --> 00:03:27,520 the end it's kind of a convenience method, right, to the printf. Or in printf we 74 00:03:27,520 --> 00:03:30,710 were doing the %n we don't need to do that with println, so let's just do that. 75 00:03:30,710 --> 00:03:36,330 So we're saying we are making a new Pez dispenser. 76 00:03:39,050 --> 00:03:40,520 Okay, so let's do it. 77 00:03:40,520 --> 00:03:44,310 Let's make a new file called PezDispenser.java. 78 00:03:44,310 --> 00:03:47,382 So what we'll do is we'll right click over here and we'll say new file, and 79 00:03:47,382 --> 00:03:53,435 we'll make a Pez dispenser and the case matters PezDispenser.java. 80 00:03:53,435 --> 00:03:56,145 All right, so let's create this class. 81 00:03:56,145 --> 00:04:01,395 So the class keyword allows us to specify that this is in fact the class. 82 00:04:01,395 --> 00:04:04,475 So class and then it's this is similar to camel case but 83 00:04:04,475 --> 00:04:08,950 the first letter is capitalized, so we're gonna do PezDispenser. 84 00:04:08,950 --> 00:04:12,340 And then we're going to open and close. 85 00:04:12,340 --> 00:04:14,050 Now believe it or not. 86 00:04:14,050 --> 00:04:17,050 That's actually enough to allow us to create an object. 87 00:04:17,050 --> 00:04:20,775 But let's add a little bit more info, so that we can show that things are working. 88 00:04:20,775 --> 00:04:21,435 Let's add some state. 89 00:04:21,435 --> 00:04:25,755 How about the name of the character whose head appears under the 'spence, all right. 90 00:04:25,755 --> 00:04:29,445 So what we'll do is we'll add a field or a member of variable. 91 00:04:29,445 --> 00:04:33,155 So anything between these two brackets. 92 00:04:33,155 --> 00:04:35,615 This block of code it's known as a class scope. 93 00:04:35,615 --> 00:04:38,885 So we'll do in here will just create a new variable and this will look very familiar. 94 00:04:38,885 --> 00:04:39,875 We'll make a new String. 95 00:04:41,200 --> 00:04:45,800 And it's a character name and we'll set that to Yoda. 96 00:04:45,800 --> 00:04:51,590 You've seen blocks used to define scope like when we're doing a while loop or 97 00:04:51,590 --> 00:04:53,010 a conditional. 98 00:04:53,010 --> 00:04:57,190 Opening a block with a curly brace and then you close it. 99 00:04:57,190 --> 00:04:59,780 It opens a new scope and classes aren't any different but 100 00:04:59,780 --> 00:05:01,140 we'll explore this more here in a bit. 101 00:05:02,190 --> 00:05:05,410 So, first though let's go use our class so remember to save this file, 102 00:05:05,410 --> 00:05:08,050 and we'll go over here to example. 103 00:05:08,050 --> 00:05:14,480 And let's make a brand new object using our Pez dispenser class as the blueprint. 104 00:05:14,480 --> 00:05:19,230 So first what we do is we declare the type of variables of the type of variable like 105 00:05:19,230 --> 00:05:20,060 normally we would do it. 106 00:05:20,060 --> 00:05:22,050 String here we're going to do a Pez dispenser. 107 00:05:24,160 --> 00:05:26,090 And then we're gonna name a variable like we always do. 108 00:05:26,090 --> 00:05:27,530 Let's call it dispenser. 109 00:05:28,960 --> 00:05:33,590 And here we're going to be creating a brand new one. 110 00:05:33,590 --> 00:05:36,230 So we're gonna use the keyword new. 111 00:05:36,230 --> 00:05:38,560 So a new PezDispenser. 112 00:05:40,350 --> 00:05:44,040 And we're going to open it and close it just like that. 113 00:05:44,040 --> 00:05:48,890 So the out object on system so System.out. 114 00:05:48,890 --> 00:05:51,160 Also has the printf method that we've been using. 115 00:05:51,160 --> 00:05:56,550 So we say printf and then we're going to put in our format strings. 116 00:05:56,550 --> 00:06:00,660 So we're gonna say the dispenser is and 117 00:06:00,660 --> 00:06:03,240 we're gonna put in a placeholder to replace there. 118 00:06:03,240 --> 00:06:07,350 So let's say %s and then remember printf doesn't automatically put the new ones in 119 00:06:07,350 --> 00:06:11,620 for us, so we need to do %n the new line, okay. 120 00:06:11,620 --> 00:06:16,760 And then, we can access the actual field that we put over there 121 00:06:16,760 --> 00:06:18,180 by saying dispenser. 122 00:06:18,180 --> 00:06:21,180 And we're going to use the dot notation on this new object that we created 123 00:06:21,180 --> 00:06:24,550 to say .characterName. 124 00:06:24,550 --> 00:06:27,040 And we'll close that system printer, all right. 125 00:06:27,040 --> 00:06:28,340 So let's go over this really quick. 126 00:06:28,340 --> 00:06:30,840 So this line here. 127 00:06:30,840 --> 00:06:36,500 We instantiate a new PezDispenser object and it creates a new 128 00:06:36,500 --> 00:06:42,518 instance this object here is referred to as an instance of type PezDispenser. 129 00:06:42,518 --> 00:06:49,000 Now because PezDispenser.java is in the same folder as our Example.java and 130 00:06:49,000 --> 00:06:52,350 we haven't done anything yet to package up our code, we'll do that later. 131 00:06:52,350 --> 00:06:56,180 We don't need to actually import PezDispenser, it's just there and 132 00:06:56,180 --> 00:06:59,110 that's because they're in the same folder. 133 00:06:59,110 --> 00:07:03,810 So our example program can simply just access the class by using its name. 134 00:07:03,810 --> 00:07:06,640 We'll talk more about packaging and importing later on. 135 00:07:06,640 --> 00:07:09,100 So but for now let's just run this. 136 00:07:09,100 --> 00:07:13,020 So let's come down here and we'll say clear and javac. 137 00:07:13,020 --> 00:07:18,192 We're just gonna compile Example.java and then 138 00:07:18,192 --> 00:07:24,000 we're gonna run example the program Example Cool. 139 00:07:24,000 --> 00:07:27,690 So it says here's a print line adding the PezDispenser to add the new line and 140 00:07:27,690 --> 00:07:31,630 that's that the PezDispenser is Yoda and it's pulling off that character name. 141 00:07:31,630 --> 00:07:34,280 Did you notice how the PezDispenser got automatically compiled, so 142 00:07:34,280 --> 00:07:39,035 if we refresh over here, you'll see that there is a PezDispenser 143 00:07:39,035 --> 00:07:43,105 class I got automatically compiled without us needing to call the javac on it. 144 00:07:43,105 --> 00:07:44,645 Now that's because in order for 145 00:07:44,645 --> 00:07:48,235 Example.java to use it it needed to have access to the PezDispenser code. 146 00:07:48,235 --> 00:07:49,925 So it compiled it. 147 00:07:49,925 --> 00:07:51,615 So this is looking pretty good, right? 148 00:07:51,615 --> 00:07:54,935 So we created a new object named dispenser and 149 00:07:54,935 --> 00:07:58,943 accessed its characterName field which was exposed. 150 00:07:58,943 --> 00:08:04,880 What I wonder if we could actually change that character name. 151 00:08:04,880 --> 00:08:06,490 That would be bad wouldn't it? 152 00:08:06,490 --> 00:08:09,690 You can't really change the PezDispenser's character after it's been created. 153 00:08:10,740 --> 00:08:14,310 So let's go ahead and let's try to change that. 154 00:08:14,310 --> 00:08:18,950 Let's change it to Darth Vader and 155 00:08:20,270 --> 00:08:24,400 see if we can't cause a disturbance in reality. 156 00:08:24,400 --> 00:08:26,110 So we'll say Darth Vader. 157 00:08:28,570 --> 00:08:32,033 And then let's go ahead we'll save this and then run it. 158 00:08:34,759 --> 00:08:37,700 No! 159 00:08:37,700 --> 00:08:38,520 We better fix that soon. 160 00:08:39,800 --> 00:08:40,650 >> Awesome. 161 00:08:40,650 --> 00:08:45,440 Now we have a blueprint, or class, that we can use to finish modeling our example. 162 00:08:45,440 --> 00:08:48,530 There are a few bad practices in what we're doing right now, but 163 00:08:48,530 --> 00:08:50,470 it is a great starting block. 164 00:08:50,470 --> 00:08:54,760 We learned about fields and how we can access them on newly created objects, or 165 00:08:54,760 --> 00:08:57,350 as they're often referred to, instances. 166 00:08:57,350 --> 00:09:00,850 So, in order to hide the inner workings of the class. 167 00:09:00,850 --> 00:09:03,770 So we can protect the Pez Dispenser head swapping problem. 168 00:09:03,770 --> 00:09:06,560 We're going to need to pick up a few new tricks which we'll get to in 169 00:09:06,560 --> 00:09:07,170 the next video. 170 00:09:08,440 --> 00:09:10,070 Now before you ask, 171 00:09:10,070 --> 00:09:14,140 when would we ever need to create a Pez Dispenser in code in real life? 172 00:09:14,140 --> 00:09:16,840 Let me answer you with this you'd be surprised. 173 00:09:18,100 --> 00:09:22,290 As a developer your skills are needed by all sorts of industries and markets. 174 00:09:22,290 --> 00:09:24,140 Everyone wants a website. 175 00:09:24,140 --> 00:09:25,630 Everyone wants an app. 176 00:09:26,840 --> 00:09:28,590 When researching Pez for this course, 177 00:09:28,590 --> 00:09:31,940 I found that there is actually a new site that is allowing you to 3 D printer 178 00:09:31,940 --> 00:09:36,180 your head onto a Pez Dispenser. Guess what they have on their website? 179 00:09:36,180 --> 00:09:37,980 a way for you to upload your head and 180 00:09:37,980 --> 00:09:41,490 simulate a Pez Dispenser to see what it might look like. 181 00:09:41,490 --> 00:09:44,030 Someone had to write that. 182 00:09:44,030 --> 00:09:46,540 Now one time when I was doing some consulting work, 183 00:09:46,540 --> 00:09:50,390 I had to write code to simulate a go cart going around a track. 184 00:09:52,290 --> 00:09:52,910 It gives me an idea. 185 00:09:53,990 --> 00:09:57,083 Let's do a quick exercise to check our syntax on creating classes and 186 00:09:57,083 --> 00:09:58,211 then go fix those books.