1 00:00:00,600 --> 00:00:02,730 ‫Instructor: Hi, within this lecture, 2 00:00:02,730 --> 00:00:06,720 ‫we're going to learn about getters and setters. 3 00:00:06,720 --> 00:00:09,600 ‫So when we deal with classes, 4 00:00:09,600 --> 00:00:12,300 ‫we generally deal with constructors 5 00:00:12,300 --> 00:00:16,320 ‫and we also deal with concepts called getters and setters. 6 00:00:16,320 --> 00:00:19,770 ‫So what does getters and setters do? 7 00:00:19,770 --> 00:00:24,770 ‫So let me change this address to private again, 8 00:00:24,960 --> 00:00:27,720 ‫like we did in the previous lecture. 9 00:00:27,720 --> 00:00:29,880 ‫And remember, when we did that, 10 00:00:29,880 --> 00:00:34,800 ‫we couldn't reach homer.age here, 11 00:00:34,800 --> 00:00:38,460 ‫not for changing and not for reading as well. 12 00:00:38,460 --> 00:00:41,700 ‫As you can see, homer.age is not reachable, 13 00:00:41,700 --> 00:00:45,090 ‫not for printing out here as well. 14 00:00:45,090 --> 00:00:47,580 ‫Maybe if we want to print out this, 15 00:00:47,580 --> 00:00:49,860 ‫maybe we want to read the value 16 00:00:49,860 --> 00:00:54,750 ‫but maybe we don't want anyone to change this value. 17 00:00:54,750 --> 00:00:57,840 ‫That's what getters and setters do. 18 00:00:57,840 --> 00:01:00,330 ‫When we define getters, 19 00:01:00,330 --> 00:01:04,410 ‫we actually can provide tools 20 00:01:04,410 --> 00:01:08,970 ‫in order to read those values, get those values. 21 00:01:08,970 --> 00:01:10,890 ‫And if we provide setters, 22 00:01:10,890 --> 00:01:15,240 ‫then someone, some developer can actually change the value 23 00:01:15,240 --> 00:01:17,910 ‫of those attributes as well. 24 00:01:17,910 --> 00:01:20,580 ‫In order to create getters and setters, 25 00:01:20,580 --> 00:01:23,850 ‫you can right click and say Generate one more time, 26 00:01:23,850 --> 00:01:26,070 ‫and choose getters and setters, 27 00:01:26,070 --> 00:01:29,400 ‫and then choose everything from that list. 28 00:01:29,400 --> 00:01:30,843 ‫And click OK. 29 00:01:31,685 --> 00:01:34,590 ‫And as you can see, it creates some methods. 30 00:01:34,590 --> 00:01:39,450 ‫So if you look at the methods closely, it's fairly easy. 31 00:01:39,450 --> 00:01:41,700 ‫You can create it on your own. 32 00:01:41,700 --> 00:01:43,290 ‫It says that public String, 33 00:01:43,290 --> 00:01:46,110 ‫so it's returning a string. 34 00:01:46,110 --> 00:01:49,950 ‫And in this case, it's returning name. 35 00:01:49,950 --> 00:01:51,570 ‫Even though it's private, 36 00:01:51,570 --> 00:01:53,130 ‫we can reach it from here 37 00:01:53,130 --> 00:01:56,280 ‫because we are in the same class, right? 38 00:01:56,280 --> 00:01:58,500 ‫So it's fairly easy. 39 00:01:58,500 --> 00:02:01,740 ‫We are getting the name and we are setting the name. 40 00:02:01,740 --> 00:02:06,740 ‫So as long as these getters and setters are public, 41 00:02:06,780 --> 00:02:09,810 ‫we can actually reach those methods 42 00:02:09,810 --> 00:02:13,650 ‫from MainActivity as well. 43 00:02:13,650 --> 00:02:16,140 ‫So if we don't make them private, 44 00:02:16,140 --> 00:02:18,240 ‫then we can reach those. 45 00:02:18,240 --> 00:02:20,880 ‫And it doesn't make sense to make getters 46 00:02:20,880 --> 00:02:23,760 ‫and setters private, okay? 47 00:02:23,760 --> 00:02:26,310 ‫And if you come over here, 48 00:02:26,310 --> 00:02:27,630 ‫we're still getting an error 49 00:02:27,630 --> 00:02:32,630 ‫because we cannot reach name from the MainActivity 50 00:02:33,300 --> 00:02:38,300 ‫since it's private, but we can reach getName method, right? 51 00:02:38,400 --> 00:02:41,100 ‫So rather than printing out this, 52 00:02:41,100 --> 00:02:45,040 ‫I'm gonna print out the homer.getName. 53 00:02:46,470 --> 00:02:49,590 ‫So as you can see, now we see the methods as well, 54 00:02:49,590 --> 00:02:52,530 ‫like getJob, getAge, getName. 55 00:02:52,530 --> 00:02:55,980 ‫Now if I do that, I won't get any error. 56 00:02:55,980 --> 00:03:00,480 ‫And if I run this, now I can reach the Home name 57 00:03:00,480 --> 00:03:02,133 ‫and I can print it out. 58 00:03:03,120 --> 00:03:06,960 ‫So maybe you want somebody to read those values 59 00:03:06,960 --> 00:03:10,200 ‫but you don't want anything, anyone 60 00:03:10,200 --> 00:03:12,750 ‫to change those values. 61 00:03:12,750 --> 00:03:15,810 ‫For example, I can actually change the name 62 00:03:15,810 --> 00:03:18,540 ‫of the Homer to like Homer Simpson 63 00:03:18,540 --> 00:03:20,490 ‫with setName method. 64 00:03:20,490 --> 00:03:22,830 ‫And if I print out one more time 65 00:03:22,830 --> 00:03:25,620 ‫by using homer.getName, 66 00:03:25,620 --> 00:03:30,127 ‫I can see Homer and then Homer Simpson consecutively 67 00:03:30,990 --> 00:03:34,950 ‫in my Logcat like this. 68 00:03:34,950 --> 00:03:39,300 ‫So if I don't want anyone to change the name 69 00:03:39,300 --> 00:03:41,730 ‫once they have initialized it, 70 00:03:41,730 --> 00:03:44,550 ‫then I can delete these setters. 71 00:03:44,550 --> 00:03:47,850 ‫I can only have the getters over here 72 00:03:47,850 --> 00:03:51,000 ‫or I can do vice versa for some reason. 73 00:03:51,000 --> 00:03:54,690 ‫Okay, so this is what getters and setters are for. 74 00:03:54,690 --> 00:03:59,610 ‫So if you see getters and setters, don't be surprised. 75 00:03:59,610 --> 00:04:01,500 ‫They're not complicated. 76 00:04:01,500 --> 00:04:06,300 ‫They're actually very, very easy but they are useful tools. 77 00:04:06,300 --> 00:04:08,160 ‫So when you deal with a class, 78 00:04:08,160 --> 00:04:10,320 ‫when you deal with a model, 79 00:04:10,320 --> 00:04:12,000 ‫if you create some attributes, 80 00:04:12,000 --> 00:04:14,640 ‫if you create some constructors, 81 00:04:14,640 --> 00:04:19,440 ‫make sure you think that if you need setters or getters. 82 00:04:19,440 --> 00:04:23,190 ‫If you need those, then just import them, 83 00:04:23,190 --> 00:04:26,730 ‫like generate them and use them in a way that you want. 84 00:04:26,730 --> 00:04:30,063 ‫If you don't need those, then of course, 85 00:04:30,063 --> 00:04:33,690 ‫you're more than welcome to ignore it as well. 86 00:04:33,690 --> 00:04:37,830 ‫And remember, we're printing out everything in this case 87 00:04:37,830 --> 00:04:40,170 ‫but in a real-life example, 88 00:04:40,170 --> 00:04:44,400 ‫we would just display them on the emulator, 89 00:04:44,400 --> 00:04:47,610 ‫on the user interface as well. 90 00:04:47,610 --> 00:04:49,140 ‫Now we're gonna stop here 91 00:04:49,140 --> 00:04:51,240 ‫and we're gonna have some challenges 92 00:04:51,240 --> 00:04:53,553 ‫and assignment in the next lecture.