1 00:00:00,840 --> 00:00:01,710 ‫Instructor: Hi. 2 00:00:01,710 --> 00:00:03,090 ‫Within this lecture, 3 00:00:03,090 --> 00:00:06,750 ‫we're going to discuss a term called scope. 4 00:00:06,750 --> 00:00:09,330 ‫So scope means range. 5 00:00:09,330 --> 00:00:12,510 ‫So why do we use that kind of a term? 6 00:00:12,510 --> 00:00:16,020 ‫Because we have different scopes over here. 7 00:00:16,020 --> 00:00:19,710 ‫Like we're creating new coding blocks with each method, 8 00:00:19,710 --> 00:00:22,470 ‫each class that we create, right? 9 00:00:22,470 --> 00:00:23,910 ‫So for example, over here, 10 00:00:23,910 --> 00:00:27,780 ‫we have defined an integer called X, 11 00:00:27,780 --> 00:00:30,780 ‫and its current value is eight. 12 00:00:30,780 --> 00:00:35,780 ‫So suppose that I want to change the value of this X. 13 00:00:36,630 --> 00:00:38,250 ‫Can I do that? 14 00:00:38,250 --> 00:00:39,450 ‫Of course I can, right? 15 00:00:39,450 --> 00:00:42,780 ‫I can just come over here and say, X is nine. 16 00:00:42,780 --> 00:00:43,613 ‫It will work, 17 00:00:43,613 --> 00:00:46,860 ‫because I haven't declared it final or anything, 18 00:00:46,860 --> 00:00:48,120 ‫it's not a constant, 19 00:00:48,120 --> 00:00:49,320 ‫it's a variable. 20 00:00:49,320 --> 00:00:51,930 ‫I can change its value. 21 00:00:51,930 --> 00:00:54,813 ‫So as you can see, value of X is nine. 22 00:00:55,680 --> 00:00:58,410 ‫But can I reach that X 23 00:00:58,410 --> 00:01:02,310 ‫from any other method like this? 24 00:01:02,310 --> 00:01:04,200 ‫Can I reach that X from here? 25 00:01:04,200 --> 00:01:07,230 ‫Like can I say X is 10? 26 00:01:07,230 --> 00:01:09,360 ‫Of course I cannot, 27 00:01:09,360 --> 00:01:12,540 ‫because it doesn't belong over here, 28 00:01:12,540 --> 00:01:13,860 ‫it doesn't belong in here. 29 00:01:13,860 --> 00:01:17,430 ‫So it's in a different scope. 30 00:01:17,430 --> 00:01:18,810 ‫If I do this, 31 00:01:18,810 --> 00:01:22,530 ‫and X is 11, or something like that, 32 00:01:22,530 --> 00:01:26,220 ‫then I can reach that X. 33 00:01:26,220 --> 00:01:27,330 ‫Nope. 34 00:01:27,330 --> 00:01:29,070 ‫I can create a new X. 35 00:01:29,070 --> 00:01:30,660 ‫I can create a new variable, 36 00:01:30,660 --> 00:01:33,330 ‫but they're not the same X, 37 00:01:33,330 --> 00:01:36,060 ‫they're not the same variable. 38 00:01:36,060 --> 00:01:41,040 ‫For example, I can write value of X in math plus X, 39 00:01:41,040 --> 00:01:42,780 ‫and run it. 40 00:01:42,780 --> 00:01:46,770 ‫And I will see the value of X will be nine, 41 00:01:46,770 --> 00:01:50,010 ‫but value of X in math will be 11. 42 00:01:50,010 --> 00:01:53,670 ‫And they're named X in both cases, 43 00:01:53,670 --> 00:01:56,220 ‫but they're completely different variables. 44 00:01:56,220 --> 00:01:59,460 ‫They are in different scopes. 45 00:01:59,460 --> 00:02:03,660 ‫So you cannot reach variable that you have defined 46 00:02:03,660 --> 00:02:06,150 ‫in one method or one scope 47 00:02:06,150 --> 00:02:08,310 ‫from another scope. 48 00:02:08,310 --> 00:02:09,420 ‫Okay? 49 00:02:09,420 --> 00:02:14,420 ‫So you have to pay attention to this range issue, 50 00:02:14,490 --> 00:02:16,860 ‫because sometimes you may want 51 00:02:16,860 --> 00:02:19,080 ‫to create the one variable 52 00:02:19,080 --> 00:02:24,080 ‫and reach that variable from all of the methods 53 00:02:24,150 --> 00:02:27,060 ‫or all of the class. 54 00:02:27,060 --> 00:02:30,000 ‫Then how do you do that? 55 00:02:30,000 --> 00:02:33,480 ‫Of course, you can use the class itself. 56 00:02:33,480 --> 00:02:36,510 ‫I'm under the main activity, okay? 57 00:02:36,510 --> 00:02:40,590 ‫But I'm not inside of any method right now. 58 00:02:40,590 --> 00:02:44,310 ‫Now I can create in any variable over here as well, 59 00:02:44,310 --> 00:02:47,850 ‫like string username, for example. 60 00:02:47,850 --> 00:02:50,620 ‫Suppose that I have a username string 61 00:02:51,657 --> 00:02:53,760 ‫and I want to reach that from every method, 62 00:02:53,760 --> 00:02:57,810 ‫because I will do something to that variable, 63 00:02:57,810 --> 00:03:02,190 ‫and I'm gonna do different things on different methods. 64 00:03:02,190 --> 00:03:05,520 ‫So right now, I have defined this username 65 00:03:05,520 --> 00:03:09,330 ‫and I'm initializing the username, 66 00:03:09,330 --> 00:03:13,140 ‫giving it a value under on create. 67 00:03:13,140 --> 00:03:15,210 ‫So whenever this activity is created, 68 00:03:15,210 --> 00:03:19,470 ‫username will be initialized with a value of James. 69 00:03:19,470 --> 00:03:22,110 ‫And I can do the same thing over here, 70 00:03:22,110 --> 00:03:24,600 ‫like username, Lars. 71 00:03:24,600 --> 00:03:25,433 ‫Okay? 72 00:03:25,433 --> 00:03:29,340 ‫Username is Kirk, for example. 73 00:03:29,340 --> 00:03:31,080 ‫And from the new methods, 74 00:03:31,080 --> 00:03:34,410 ‫maybe username something else. 75 00:03:34,410 --> 00:03:37,410 ‫Beware that I'm not using string username, 76 00:03:37,410 --> 00:03:41,280 ‫I'm not creating a new variable each time. 77 00:03:41,280 --> 00:03:46,280 ‫I'm just changing the value of the same variable, 78 00:03:46,710 --> 00:03:49,530 ‫same object that I'm referring over here, 79 00:03:49,530 --> 00:03:52,020 ‫like username, username, username. 80 00:03:52,020 --> 00:03:56,880 ‫And since I have defined this globally inside of my class, 81 00:03:56,880 --> 00:04:01,880 ‫I can reach it from anywhere I want, right? 82 00:04:01,890 --> 00:04:04,080 ‫So this is cool. 83 00:04:04,080 --> 00:04:06,090 ‫I have a variable that I can reach 84 00:04:06,090 --> 00:04:08,700 ‫from every method that I have. 85 00:04:08,700 --> 00:04:12,930 ‫And this is defining this as initializing. 86 00:04:12,930 --> 00:04:13,763 ‫Okay? 87 00:04:13,763 --> 00:04:17,550 ‫I can initialize this as an empty string, for example, 88 00:04:17,550 --> 00:04:19,710 ‫and within each method I can 89 00:04:19,710 --> 00:04:22,470 ‫give it another value, like this, 90 00:04:22,470 --> 00:04:24,300 ‫username is Lars. 91 00:04:24,300 --> 00:04:25,650 ‫If I call test method, 92 00:04:25,650 --> 00:04:27,480 ‫but username will be Kirk 93 00:04:27,480 --> 00:04:29,253 ‫if I call math methods. 94 00:04:30,150 --> 00:04:32,100 ‫Maybe you have different buttons 95 00:04:32,100 --> 00:04:34,770 ‫to assign different names to a username, 96 00:04:34,770 --> 00:04:35,940 ‫I don't know. 97 00:04:35,940 --> 00:04:38,610 ‫You can do it with this. 98 00:04:38,610 --> 00:04:39,780 ‫Okay? 99 00:04:39,780 --> 00:04:44,160 ‫So this is a very important concept. 100 00:04:44,160 --> 00:04:47,760 ‫First, we can not reach 101 00:04:47,760 --> 00:04:50,280 ‫if we declare a variable, 102 00:04:50,280 --> 00:04:54,660 ‫if we define and initialize a variable like this, okay? 103 00:04:54,660 --> 00:04:58,020 ‫In a method from anywhere else, 104 00:04:58,020 --> 00:05:02,220 ‫but we can if we define it under class. 105 00:05:02,220 --> 00:05:03,840 ‫So for example, right now, 106 00:05:03,840 --> 00:05:06,990 ‫this username is not the same username 107 00:05:06,990 --> 00:05:08,130 ‫with the other ones, 108 00:05:08,130 --> 00:05:10,410 ‫but if I just remove the string, 109 00:05:10,410 --> 00:05:13,860 ‫they will be equal to each other. 110 00:05:13,860 --> 00:05:14,850 ‫Okay? 111 00:05:14,850 --> 00:05:16,740 ‫So this is how scope works, 112 00:05:16,740 --> 00:05:20,190 ‫This is how Android Studio tries to understand 113 00:05:20,190 --> 00:05:22,290 ‫which variable is which. 114 00:05:22,290 --> 00:05:23,940 ‫So let's stop here 115 00:05:23,940 --> 00:05:26,553 ‫and continue within the next lecture.