1 00:00:00,630 --> 00:00:03,600 ‫Instructor: Hi, We're going to deep dive 2 00:00:03,600 --> 00:00:06,060 ‫into the classes in this lecture 3 00:00:06,060 --> 00:00:08,610 ‫and we're gonna learn a very important concept 4 00:00:08,610 --> 00:00:12,090 ‫called constructor, okay? 5 00:00:12,090 --> 00:00:15,690 ‫So far we have created our own object 6 00:00:15,690 --> 00:00:19,230 ‫but there are some better ways to deal with this kind of 7 00:00:19,230 --> 00:00:24,230 ‫situations in Java at our under development. 8 00:00:24,720 --> 00:00:28,140 ‫So in order to learn about constructor, 9 00:00:28,140 --> 00:00:30,000 ‫I'm just gonna create it. 10 00:00:30,000 --> 00:00:33,420 ‫So if you come over here and right click and say generate 11 00:00:33,420 --> 00:00:38,220 ‫or if you come over here to coding menu and say generate 12 00:00:38,220 --> 00:00:40,500 ‫it will give you a menu. 13 00:00:40,500 --> 00:00:41,760 ‫And as you can see 14 00:00:41,760 --> 00:00:45,720 ‫we have the constructor in the first place. 15 00:00:45,720 --> 00:00:48,810 ‫Now we are looking for constructor 16 00:00:48,810 --> 00:00:53,280 ‫because that's what we are going to learn 17 00:00:53,280 --> 00:00:55,860 ‫and deep dive in this lecture. 18 00:00:55,860 --> 00:00:59,100 ‫As you can see, once we come over here 19 00:00:59,100 --> 00:01:02,790 ‫it will ask what kind of properties that we want to include. 20 00:01:02,790 --> 00:01:07,790 ‫Hit shift and select everything and hit enter or okay. 21 00:01:08,700 --> 00:01:12,480 ‫And as you can see, it created our first constructor. 22 00:01:12,480 --> 00:01:15,090 ‫So what is a constructor anyway? 23 00:01:15,090 --> 00:01:20,070 ‫Yeah, it constructs some object, but what does it really do? 24 00:01:20,070 --> 00:01:24,690 ‫So if you create that, that constructor, 25 00:01:24,690 --> 00:01:26,400 ‫and if you come over here, 26 00:01:26,400 --> 00:01:30,390 ‫now you will see you're getting some kind of an error. 27 00:01:30,390 --> 00:01:33,300 ‫So what is the error over here? 28 00:01:33,300 --> 00:01:35,490 ‫So we are creating an instance, 29 00:01:35,490 --> 00:01:40,260 ‫an object out of this Musicians class. 30 00:01:40,260 --> 00:01:41,100 ‫Right? 31 00:01:41,100 --> 00:01:45,030 ‫So when we create a new instance or a new object 32 00:01:45,030 --> 00:01:48,990 ‫constructor is the first method to be executed. 33 00:01:48,990 --> 00:01:51,570 ‫This is the first thing happens 34 00:01:51,570 --> 00:01:56,570 ‫right after you create your own object. 35 00:01:57,210 --> 00:02:00,183 ‫And in this case, as you can see, 36 00:02:01,260 --> 00:02:06,060 ‫we are doing some kind of initialization like 37 00:02:06,060 --> 00:02:08,820 ‫this.name = name. 38 00:02:08,820 --> 00:02:11,430 ‫So let me print out something over here, 39 00:02:11,430 --> 00:02:14,370 ‫so we can understand it better, right? 40 00:02:14,370 --> 00:02:18,780 ‫So as you can see, we have all this codes here. 41 00:02:18,780 --> 00:02:21,000 ‫this.instrument = instrument 42 00:02:21,000 --> 00:02:23,160 ‫this.age = age. 43 00:02:23,160 --> 00:02:26,880 ‫And it doesn't make sense when you first look into it. 44 00:02:26,880 --> 00:02:30,030 ‫However, it actually makes sense. 45 00:02:30,030 --> 00:02:33,720 ‫Since we have these properties inside of our class. 46 00:02:33,720 --> 00:02:36,270 ‫Now we can ask for an input 47 00:02:36,270 --> 00:02:40,350 ‫as a parameter inside of our public Musicians method. 48 00:02:40,350 --> 00:02:45,060 ‫And then we can make this.name 49 00:02:45,060 --> 00:02:47,940 ‫equals to that parameter. 50 00:02:47,940 --> 00:02:50,580 ‫You don't know what this is. 51 00:02:50,580 --> 00:02:52,620 ‫I'm going to explain it, don't worry. 52 00:02:52,620 --> 00:02:54,660 ‫Right now focus on this. 53 00:02:54,660 --> 00:02:57,540 ‫We're going to take some parameters 54 00:02:57,540 --> 00:03:01,770 ‫and we're gonna make it equal to the properties 55 00:03:01,770 --> 00:03:04,770 ‫that we have defined inside of our class. 56 00:03:04,770 --> 00:03:08,933 ‫Now you can change it if you want, like instrument2, age2, 57 00:03:10,170 --> 00:03:15,170 ‫and you can actually differentiate these names 58 00:03:15,180 --> 00:03:17,580 ‫from these parameters. 59 00:03:17,580 --> 00:03:22,260 ‫So name over here, this.name refers 60 00:03:22,260 --> 00:03:25,830 ‫to the attributes of this class. 61 00:03:25,830 --> 00:03:29,460 ‫So this is, these are called attributes 62 00:03:29,460 --> 00:03:31,470 ‫and these are called parameters. 63 00:03:31,470 --> 00:03:33,360 ‫So we are asking for parameters 64 00:03:33,360 --> 00:03:37,980 ‫inside of any other class inside a main activity 65 00:03:37,980 --> 00:03:42,690 ‫and developer gives some instrument2, name2, age2 66 00:03:42,690 --> 00:03:47,130 ‫And by saying this.name, we refer to the name, 67 00:03:47,130 --> 00:03:50,160 ‫instrument, and age of the class. 68 00:03:50,160 --> 00:03:53,370 ‫So this refers to the class, 69 00:03:53,370 --> 00:03:56,370 ‫this refers musicians. 70 00:03:56,370 --> 00:04:00,780 ‫You can name them, name, instrument, or age 71 00:04:00,780 --> 00:04:03,060 ‫and you can name them whatever you want. 72 00:04:03,060 --> 00:04:05,760 ‫But this is the main convention. 73 00:04:05,760 --> 00:04:08,850 ‫They have the same names. 74 00:04:08,850 --> 00:04:11,520 ‫So you can do it like this. 75 00:04:11,520 --> 00:04:14,190 ‫Let me call this constructor called 76 00:04:14,190 --> 00:04:16,110 ‫and let me come over here 77 00:04:16,110 --> 00:04:19,590 ‫and see what kind of error we have and as you can see 78 00:04:19,590 --> 00:04:23,310 ‫Musicians now asking for parameter 79 00:04:23,310 --> 00:04:25,410 ‫and we don't give, 80 00:04:25,410 --> 00:04:28,920 ‫if we don't give any parameters, it won't even work. 81 00:04:28,920 --> 00:04:33,750 ‫Now we have to give age, instrument and name 82 00:04:33,750 --> 00:04:37,680 ‫rather than giving those values afterwards. 83 00:04:37,680 --> 00:04:40,650 ‫When we say new Musicians, it'll ask for a name 84 00:04:40,650 --> 00:04:45,650 ‫and as you can see, it actually gives us some hints as well. 85 00:04:46,140 --> 00:04:49,560 ‫Once we hit double quotation marks, 86 00:04:49,560 --> 00:04:52,230 ‫now I gotta say James, Guitar and 50, 87 00:04:52,230 --> 00:04:57,230 ‫and it'll just make it equal to James, Guitar and 50. 88 00:04:58,260 --> 00:05:01,920 ‫Now if I say james.age one more time 89 00:05:01,920 --> 00:05:05,070 ‫I can reach that value as well, right? 90 00:05:05,070 --> 00:05:07,860 ‫So let me try to print this out. 91 00:05:07,860 --> 00:05:12,360 ‫I will say james.instrument, for example. 92 00:05:12,360 --> 00:05:15,060 ‫It'll print out guitar. 93 00:05:15,060 --> 00:05:17,910 ‫Of course, in order to make this work, 94 00:05:17,910 --> 00:05:21,690 ‫I have to call this makeMusicians Method 95 00:05:21,690 --> 00:05:24,810 ‫inside of any other method like onCreate 96 00:05:24,810 --> 00:05:27,660 ‫or any other method that will be executed 97 00:05:27,660 --> 00:05:29,940 ‫without my intervention. 98 00:05:29,940 --> 00:05:34,920 ‫So let me call this inside of onCreate methods, 99 00:05:34,920 --> 00:05:36,540 ‫like here, okay? 100 00:05:36,540 --> 00:05:38,160 ‫And this will be called, 101 00:05:38,160 --> 00:05:40,650 ‫and we are gonna create an instance, 102 00:05:40,650 --> 00:05:44,310 ‫and then we are going to print out the instrument 103 00:05:44,310 --> 00:05:46,440 ‫of that instance. 104 00:05:46,440 --> 00:05:48,990 ‫So let's follow this. 105 00:05:48,990 --> 00:05:49,980 ‫Here we go. 106 00:05:49,980 --> 00:05:53,910 ‫Now constructor called because we created James, 107 00:05:53,910 --> 00:05:56,084 ‫and then after that 108 00:05:56,084 --> 00:06:01,084 ‫the instrument of James got printed in the logs. 109 00:06:01,260 --> 00:06:05,220 ‫So this is how you work with constructors. 110 00:06:05,220 --> 00:06:08,100 ‫This is how you work with classes generally. 111 00:06:08,100 --> 00:06:10,983 ‫So let's stop here and continue within the next one.