The scope of a variable can be at the level of a class or a function.
So far, you learned about classes and functions, the class contains all of your code and each function
contains a small grouping of your code.
When you make a variable, it can exist at the level of a class or at the level of a function.
So in this lesson, you're going to learn about function, scope and scope.
First thing you'll need to do is create a new class by yourself inside the section for a project, create
a new file name scoped out Java, and make sure the class has a main method.
The scope of a function spans the two curly brackets, a variable inside the function can only exist
in the function scope.
When this line of code runs, Java creates the variable and when the function ends, that variable falls
out of scope, it ceases to exist outside of the function scope.
It's gone.
And so inside, man, I'm going to make it into a variable named Appel's.
And now I'm going to make a function called some function, this function is void.
It's not going to return anything.
And it will take no parameters.
Now I'm going to call some function from within men.
And inside some function, I'm going to try to print the apples variable that I declared inside men.
Running my code.
And obviously, it doesn't compile, you cannot access a variable from outside of its scope, you created
the variable inside main, the variable only exists inside the main scope.
So if you look at the image, I highlighted the scope for you.
It starts at the first curly brace and it ends at the second curly brace.
And you cannot access the variable from outside of the scope where you created it.
Instead, we need to write the variable inside some function.
Now we can recompile and run the code.
And it prince 5:00, no surprise there.
And now try printing the variable for Min.
And the code doesn't compile, as I just said, you cannot access a variable beyond the scope of its
existence, this variable only exists at the scope of some function, and you cannot access it from
the scope of another function.
Now, a variable in a class exists inside the class scope and the scope of a class spends all of its
code.
If you think of scope as going from the first curly bracket to the second curly bracket, it makes sense
that any variable that you declare at the scope of the class is available anywhere inside the class.
In this case, I declare the variable dogs at the level of the class and have a look.
I can access and print it from anywhere inside the class.
OK, now remove the code from earlier.
But leave the some function call, and now we're going to make it a variable dogs at the level of the
class and set it equal to five.
Now, the scope of dogs spans the entire class, so we're going to print it from the main function.
As well as from some function and the only reason we're still getting an error is because we have to
declare the variable a static, since we're calling it from static methods.
Again, don't worry about what static is for now, we're going to dive deeper into it in module two,
just know that every variable and every function at the class level needs to be static.
OK, rewriting our code.
And it prints five on both occasions, the scope of dogs is at the class level, which means we can
access it from anywhere inside the class.
In this lesson, you learned about function, scope and scope, when you make a variable inside a function,
it only exists within the function scope.
And when you make a verbal at the level of the class, you can access it from anywhere inside the class.