1 00:00:00,421 --> 00:00:03,997 We've typed some code and we've seen how to run our project. 2 00:00:03,997 --> 00:00:08,534 Let's take things a step further and try creating a new class. 3 00:00:08,534 --> 00:00:14,938 In the project pane, right com.teamtreehouse and 4 00:00:14,938 --> 00:00:18,146 choose New > Java Class. 5 00:00:18,146 --> 00:00:25,103 Then let's name it Person, leave the kind as Class and hit OK. 6 00:00:25,103 --> 00:00:26,075 Awesome. 7 00:00:26,075 --> 00:00:31,352 Now we've got a person.java file inside our com.teamtreehouse folders. 8 00:00:31,352 --> 00:00:35,235 And it even went ahead and added the package statement, 9 00:00:35,235 --> 00:00:39,530 as well as the code declaring the person class. 10 00:00:39,530 --> 00:00:42,570 I know we've talked a little about package statements, but 11 00:00:42,570 --> 00:00:45,790 this might be a good time to talk about them a little more. 12 00:00:45,790 --> 00:00:49,720 Package statements, like the one on line one, 13 00:00:49,720 --> 00:00:53,070 make sure our class names are distinguishable. 14 00:00:53,070 --> 00:00:58,120 For example, if we tried to make another person class, 15 00:00:58,120 --> 00:01:02,533 we get an error and not be able to make that class. 16 00:01:02,533 --> 00:01:06,960 However, if we first create another package by 17 00:01:06,960 --> 00:01:11,169 right-clicking on com.teamtreehouse and 18 00:01:11,169 --> 00:01:17,147 choosing New > Package and then naming it something like data, 19 00:01:17,147 --> 00:01:22,700 we can now create another person class and our new package. 20 00:01:26,089 --> 00:01:28,853 Also, we may be calling this a package. 21 00:01:28,853 --> 00:01:32,497 But remember, behind the scenes, it's really just a folder. 22 00:01:35,415 --> 00:01:38,925 And if we open it, we'll find our new person class. 23 00:01:38,925 --> 00:01:42,482 All right, now, we don't really need two person classes. 24 00:01:42,482 --> 00:01:46,889 So let's delete the original one by selecting it in the project pane and 25 00:01:46,889 --> 00:01:47,997 hitting Delete. 26 00:01:51,311 --> 00:01:53,908 And hit OK to finish deleting it. 27 00:01:53,908 --> 00:01:59,014 Then, inside our remaining person class, let's declare 28 00:01:59,014 --> 00:02:05,049 a string field called name and initialize it by adding a constructor. 29 00:02:05,049 --> 00:02:09,658 So person, And 30 00:02:09,658 --> 00:02:13,968 we'll need it to take in the name variable. 31 00:02:13,968 --> 00:02:17,614 And inside the constructor we'll want to set our name field. 32 00:02:17,614 --> 00:02:22,872 So this.name equals our name parameter. 33 00:02:22,872 --> 00:02:27,973 Then let's add a new get name function and have it return the name field. 34 00:02:27,973 --> 00:02:32,010 While we're returning a string, called the method getName. 35 00:02:37,120 --> 00:02:39,769 And inside, we'll just return our name field. 36 00:02:40,950 --> 00:02:46,260 Here we can see a couple more ways, IntelliJ helps us write good code. 37 00:02:46,260 --> 00:02:50,120 Notice that the person class, person contractor, and 38 00:02:50,120 --> 00:02:52,230 getName function are all grade out. 39 00:02:53,280 --> 00:02:57,872 This is IntelliJ's way of telling us this code isn't currently being used. 40 00:02:57,872 --> 00:03:00,922 Though we'll be using it in just a little bit. 41 00:03:00,922 --> 00:03:04,901 Also, notice that the name field is highlighted in yellow. 42 00:03:04,901 --> 00:03:08,972 If we hover our mouse over it, it says, access can be private. 43 00:03:08,972 --> 00:03:13,647 Things highlighted in yellow are just warnings, so we don't have to fix them. 44 00:03:13,647 --> 00:03:19,307 However, this does give us an opportunity to show off another feature of IntelliJ. 45 00:03:19,307 --> 00:03:21,372 Let's click on the name field and 46 00:03:21,372 --> 00:03:25,500 then use Alt + Enter to bring up the quick fix menu. 47 00:03:25,500 --> 00:03:28,960 Any time you have an error or warning in your code, 48 00:03:28,960 --> 00:03:34,150 you can use Alt + Enter and IntelliJ will try its best to give you the solution. 49 00:03:34,150 --> 00:03:39,320 Since we've already got make private selected, let's just hit Enter, and 50 00:03:39,320 --> 00:03:43,180 there we go, we've got a private field and no more warning. 51 00:03:43,180 --> 00:03:45,670 Another thing to point out is that fields or 52 00:03:45,670 --> 00:03:50,020 member variables look different from local function variables. 53 00:03:50,020 --> 00:03:54,945 So we can easily tell that this name is different than this name. 54 00:03:54,945 --> 00:04:00,283 All right, let's flip back to Main.java and start using our person class. 55 00:04:00,283 --> 00:04:06,680 Let's delete our print statement and instead create a new person object. 56 00:04:06,680 --> 00:04:13,393 Let's type Person, and then leave a space, which gives us an error. 57 00:04:13,393 --> 00:04:19,693 If we put our mouse over it, it says cannot resolve symbol person. 58 00:04:19,693 --> 00:04:24,808 This is because the person class is in a different package than our main class. 59 00:04:24,808 --> 00:04:28,631 So before we can use this person class, we'll need to import it. 60 00:04:28,631 --> 00:04:33,761 Luckily, rather than typing out the entire import statement, 61 00:04:33,761 --> 00:04:37,567 we can just click on person and use Alt + Enter. 62 00:04:37,567 --> 00:04:43,202 Also, when we were first typing out person, instead of typing it all out, 63 00:04:46,013 --> 00:04:49,662 If we just used Enter to accept code completion, 64 00:04:49,662 --> 00:04:52,339 it automatically adds the import. 65 00:04:52,339 --> 00:04:53,601 Pretty cool? 66 00:04:53,601 --> 00:04:58,225 Now that we've got our person class imported, let's finish creating a person. 67 00:04:58,225 --> 00:05:02,707 So person, and we'll call them person. 68 00:05:02,707 --> 00:05:07,188 Set them equal to a new_Person. 69 00:05:07,188 --> 00:05:13,011 And I'll name mine Ben because that's my name. 70 00:05:13,011 --> 00:05:16,633 Unfortunately, this gives us another error. 71 00:05:16,633 --> 00:05:19,264 It looks like since we're in another package, 72 00:05:19,264 --> 00:05:22,166 we'll need to make our person constructor public. 73 00:05:22,166 --> 00:05:25,614 Which we can pretty easily do with Alt+Enter. 74 00:05:28,306 --> 00:05:32,841 And if we look in the person class, we can see that it now has a public constructor. 75 00:05:36,141 --> 00:05:42,010 On the next line, let's declare a new string variable 76 00:05:42,010 --> 00:05:47,377 called name and set it equal to person.getName. 77 00:05:49,506 --> 00:05:52,656 Using Alt + Enter again to make the function public. 78 00:05:54,941 --> 00:05:59,533 Finally, on the next line, let's print out the name variable, 79 00:05:59,533 --> 00:06:04,139 but this time, instead of typing it out, let's use a shortcut. 80 00:06:04,139 --> 00:06:09,140 Just type S-O-U-T and hit Enter. 81 00:06:09,140 --> 00:06:12,722 Then pass in our name variable, and there we go. 82 00:06:12,722 --> 00:06:16,364 There's a few shortcuts like this in IntelliJ but 83 00:06:16,364 --> 00:06:19,579 S-O-U-T is definitely the most useful. 84 00:06:19,579 --> 00:06:24,849 And if we run the program again, it prints out our name variable. 85 00:06:24,849 --> 00:06:25,788 Awesome. 86 00:06:25,788 --> 00:06:26,835 In the next video, 87 00:06:26,835 --> 00:06:30,640 we'll keep mastering IntelliJ by learning how to use the debugger.