#5 Scope

In this tutorial we’re going to talk a bit about something called scope. So let’s get started by closing our methods.java file, right click in the default package selecting new and class. For the class name I’m going to type scope, and click Finish. Let’s go ahead and create our main method as usual. Type the word main press Ctrl-spacebar and hit enter.

Now that you hopefully have a little experience with methods it’s important to understand how where you declare your variables effects where you can use them.

To demonstrate this I’m going to create a new method after main.

I’m going to type a static void do something parentheses open curly brace, enter

now.

p49.jpg

Every method including the main method can have what are called local variables.

And as the name implies this means that the variables are local to the method in which they were declared. What that means is that you can access variables declared in a method from many other methods.

So for example if we were to declare and initialize a variable called X in Maine there is no way that our method would do something and can access that data.

If we were to try and say X equals 10, Eclipse would tell us something’s wrong and says X cannot be resolved to a variable.

p50.jpg

And what that essentially means is that the variable x is not in scope when this method runs. But what if that’s not what we wanted. What if we wanted multiple methods to work with the same data without passing it around into the methods.

Well there’s a way to do that. And what we have to do is to declare the variable outside of the methods. This is usually done at the very top of the class.

I’m going to go to the Curly Brace. After public class scope and hit enter a couple times.

So now we’re above the main method we’re outside of any method.

And I’m going to type: static int x. Now we have a variable called x that is declared outside of any methods. And what that allows us to do is to use it inside of any method.

So if we take the int off of this X now if main runs, X will be assigned a value 5 and now when do something runs X we’ll be assigned the value 10.

So to demonstrate that let’s do our system out type as well. So press Ctrl-spacebar and have it print out X, then we’ll call our do something method. And once again we’ll print out x. Store it. Copy paste that line and let’s run it

p51.jpg

and as we’ll see five is printed out and then 10 is printed out.

Let’s put a comment on our variable declaration to remind us of how that works.

So I’m going to say

//because X is declared outside of any method it is in scope to all methods. In other words // any method can access X.

I’m going to go down after our do something method and create a new method called static void to do something locally. Parentheses opening clearly braces. Now I’m going to declare another variable let’s say and y equals 100. I’m going to put a comment on this one as well. I’ll say

// because why is declared inside of this method. it is local to this method. In other words //no other method has access to y.

Now inside of this method we can do anything we want with Y.

But once this method finishes executing y essentially ceases to exist.

In other words not only can y only be accessed inside of or do something locally method after it is declared but it doesn’t even exist, after that method is finished executing

now eventually in these tutorials We’ll get to a point where we talk about something called stack frames and look at how memory is allocated when you call a method and you’ll understand this much more.

But for now just understand that there are variables that are declared outside of methods that can be used throughout the entire class and there are variables that you can declare inside of methods that are said to be local to that method.

p52

You want to be sure you understand the difference between declaring a variable and accessing a variable as well.

So on this line we are declaring the variable x and on this line we are assigning value to X and we’re also doing the same thing.

Now do something method and then do something locally a method. You are declaring and initializing a variable y.

So all that matters when we talk about scope is where the variable is declared here.

X is declared outside of any method.And here y is declared inside of the do something locally method.

I think that’s about everything I want to say about scope for now.

But one last thing is when you see a variable declared in this way where it says static type variable and it is outside of any method, those are referred to as class variables whereas variables declared inside of a method are once again local variables.

Thanks for watching.

Leave a comment