1 00:00:00,330 --> 00:00:03,540 The scope of a variable can be at the level of a class or a function. 2 00:00:05,800 --> 00:00:11,410 So far, you learned about classes and functions, the class contains all of your code and each function 3 00:00:11,410 --> 00:00:13,480 contains a small grouping of your code. 4 00:00:15,320 --> 00:00:19,760 When you make a variable, it can exist at the level of a class or at the level of a function. 5 00:00:22,450 --> 00:00:26,200 So in this lesson, you're going to learn about function, scope and scope. 6 00:00:28,590 --> 00:00:33,420 First thing you'll need to do is create a new class by yourself inside the section for a project, create 7 00:00:33,420 --> 00:00:37,590 a new file name scoped out Java, and make sure the class has a main method. 8 00:00:45,250 --> 00:00:51,070 The scope of a function spans the two curly brackets, a variable inside the function can only exist 9 00:00:51,070 --> 00:00:52,420 in the function scope. 10 00:00:54,600 --> 00:01:00,450 When this line of code runs, Java creates the variable and when the function ends, that variable falls 11 00:01:00,450 --> 00:01:04,500 out of scope, it ceases to exist outside of the function scope. 12 00:01:04,800 --> 00:01:05,370 It's gone. 13 00:01:13,460 --> 00:01:16,400 And so inside, man, I'm going to make it into a variable named Appel's. 14 00:01:21,780 --> 00:01:26,190 And now I'm going to make a function called some function, this function is void. 15 00:01:26,430 --> 00:01:27,810 It's not going to return anything. 16 00:01:35,900 --> 00:01:37,820 And it will take no parameters. 17 00:01:40,760 --> 00:01:43,010 Now I'm going to call some function from within men. 18 00:01:50,560 --> 00:01:54,940 And inside some function, I'm going to try to print the apples variable that I declared inside men. 19 00:01:59,030 --> 00:01:59,960 Running my code. 20 00:02:07,100 --> 00:02:13,430 And obviously, it doesn't compile, you cannot access a variable from outside of its scope, you created 21 00:02:13,430 --> 00:02:17,900 the variable inside main, the variable only exists inside the main scope. 22 00:02:18,470 --> 00:02:21,050 So if you look at the image, I highlighted the scope for you. 23 00:02:21,470 --> 00:02:25,280 It starts at the first curly brace and it ends at the second curly brace. 24 00:02:26,800 --> 00:02:30,640 And you cannot access the variable from outside of the scope where you created it. 25 00:02:31,630 --> 00:02:34,390 Instead, we need to write the variable inside some function. 26 00:02:36,150 --> 00:02:38,010 Now we can recompile and run the code. 27 00:02:44,120 --> 00:02:46,160 And it prince 5:00, no surprise there. 28 00:02:47,930 --> 00:02:49,850 And now try printing the variable for Min. 29 00:02:59,240 --> 00:03:05,300 And the code doesn't compile, as I just said, you cannot access a variable beyond the scope of its 30 00:03:05,300 --> 00:03:11,540 existence, this variable only exists at the scope of some function, and you cannot access it from 31 00:03:11,540 --> 00:03:12,860 the scope of another function. 32 00:03:16,970 --> 00:03:23,900 Now, a variable in a class exists inside the class scope and the scope of a class spends all of its 33 00:03:23,900 --> 00:03:24,350 code. 34 00:03:24,620 --> 00:03:29,990 If you think of scope as going from the first curly bracket to the second curly bracket, it makes sense 35 00:03:29,990 --> 00:03:35,570 that any variable that you declare at the scope of the class is available anywhere inside the class. 36 00:03:36,440 --> 00:03:40,400 In this case, I declare the variable dogs at the level of the class and have a look. 37 00:03:40,760 --> 00:03:43,700 I can access and print it from anywhere inside the class. 38 00:03:54,960 --> 00:03:56,850 OK, now remove the code from earlier. 39 00:03:58,460 --> 00:04:04,010 But leave the some function call, and now we're going to make it a variable dogs at the level of the 40 00:04:04,010 --> 00:04:06,470 class and set it equal to five. 41 00:04:09,410 --> 00:04:15,410 Now, the scope of dogs spans the entire class, so we're going to print it from the main function. 42 00:04:21,829 --> 00:04:26,990 As well as from some function and the only reason we're still getting an error is because we have to 43 00:04:26,990 --> 00:04:30,590 declare the variable a static, since we're calling it from static methods. 44 00:04:32,030 --> 00:04:35,720 Again, don't worry about what static is for now, we're going to dive deeper into it in module two, 45 00:04:36,050 --> 00:04:40,040 just know that every variable and every function at the class level needs to be static. 46 00:04:42,380 --> 00:04:43,880 OK, rewriting our code. 47 00:04:46,600 --> 00:04:52,900 And it prints five on both occasions, the scope of dogs is at the class level, which means we can 48 00:04:52,900 --> 00:04:55,030 access it from anywhere inside the class. 49 00:04:59,220 --> 00:05:03,990 In this lesson, you learned about function, scope and scope, when you make a variable inside a function, 50 00:05:03,990 --> 00:05:06,450 it only exists within the function scope. 51 00:05:11,120 --> 00:05:16,160 And when you make a verbal at the level of the class, you can access it from anywhere inside the class.