Variable Scope

Introduction

Knowing when you can use a variable gets confusing if you don't understand variable scope. Have you ever seen the same variable name in two different places in a piece of Java code, but they held different values? Have you ever tried to use a variable and had the Java compiler yell at you because you apparently have no reference to the variable in your context? Again, this all has to do with variable scope and this tutorial will help to uncover the mystery of scope in Java.

Class Level Scope

In Java, there are some variables that you want to be able to access from anywhere within a Java class. The scope of these variables will need to be at the class level, and there is only one way to create variables at that level – just inside the class but outside of any methods. Let's take a look at an example:

You will notice that the variables have been defined at the top of the class, before any methods. This is simply a convention; you can define your class-level variables anywhere in the class (so long as it's outside of any methods in the class). However, it is good practice to put these variables at the top so it's easier to see where they all are. Also note that the access identifier does not have anything to do with the variable's scope within the class! If you're not sure what access identifiers are, I strongly recommend checking out our tutorial about them by clicking here.

Method Scope

Some variables you might want to make temporary and preferably they are used for only one method. This would be an example of method scope. Here's a pretty typical example of a variable in method scope using an example of Java's main method:

The variable x in the example is created inside the method. When the method ends, the variable reference goes away and there is no way to access that variable any longer. You cannot use that same variable x in another method because it only exists in the main method's scope and that is it.

Here's another example of method scope, except this time the variable got passed in as a parameter to the method:

The above example is the typical example of a setter method. The purpose of a setter method as you might recall is to set a class variable to a particular value from somewhere outside of the class. The above example is a pretty clean example of this. Now let's look at the same example, except now we'll use a conflicting variable name.

So, what's going on with the variable scope here? And what is "this"? The this keyword helps Java to differentiate between the local scope variable (the method scope variable) and the class variable. This.username tells Java that you are referencing the class variable. So, the setter above sets the method-scope variable called name to the class variable called name. The variable scope here is not in conflict because Java knows which variable to access because of the "this" keyword. You could also do this:

However the above is something you probably won't be doing often (if at all!) because you're setting the local, temporary variable to the value of the more permanent one.

Loop Scope

Have you ever had trouble accessing a variable when dealing with for loops and while loops? Variable scope plays a big role in your difficulty, so let's figure out what's going on there.

Any variables created inside of a loop are LOCAL TO THE LOOP. This means that once you exit the loop, the variable can no longer be accessed! This includes any variables created in the loop signature. Take a look at the following two for loop examples:

In the first example, x can ONLY be used inside of the for loop. In the second example, you are free to use x inside of the loop as well as outside of the loop because it was declared outside of the loop (it has been declared at method scope).

A quick hint about variable scope in Java:

In general, a set of curly brackets { } defines a particular scope. In Java you can usually access a variable as long as it was defined within the same set of brackets as the code you are writing or within any curly brackets inside of the curly brackets where the variable was defined.

Hopefully this helps to clarify variable scope in Java. Many people get confused about this topic because it's never explained properly. If there are examples that are confusing in this tutorial, free free to contact me and let me know!

If you would like to leave a comment or have a question about our Variable Scope tutorial, please click here or visit the Contact Us sidebar link to fill out our contact us form. Thanks!