Main Page

Previous Next

Variable Scope

The scope of a variable is the part of the program over which the variable name can be referenced – in other words, where you can use the variable in the program. Every variable that we have declared so far in program examples has been defined within the context of a method, the method main(). Variables that are declared within a method are called local variables, as they are only accessible within the confines of the method in which they are declared. However, they are not necessarily accessible everywhere in the code for the method in which they are declared. Look at the example in the illustration below that shows nested blocks inside a method.

      int a = 1l                  // Declare and define a

      // Reference to a is OK here
      // Reference to be here is an error

            // Reference to a here is OK
            // Reference to b here is still an error

            int b = 2;            // Declare and define b

            // References to a and b are OK here - b exists now

      // Reference to b is an error here - it doesn't exist
      // Reference to a is still OK though

A variable does not exist before its declaration; you can only refer to it after it has been declared. It continues to exist until the end of the block in which it is defined, and that includes any blocks nested within the block containing its declaration. The variable b only exists within the inner block. After the brace at the end of the inner block, b no longer exists so you can't refer to it. The variable a is still around though since it survives until the last brace.

So, the rule for accessibility of local variables is simple. They are only accessible from the point in the program where they are declared to the end of the block that contains the declaration. At the end of the block in which they are declared they cease to exist. We can demonstrate this with an example:

Try It Out – Scoping

We will define our method main()to illustrate how variable scope works. First we declare and initialize the variable outer, then start an inner block. Within that block, we will define another variable inner. When the block closes we have to redeclare inner to use it once more.

public class Scope {
  public static void main(String[] args) {
    int outer = 1;                                // Exists throughout the method

      // You cannot refer to a variable before its declaration
      // System.out.println("inner = " + inner);  // Uncomment this for an error

      int inner = 2;
      System.out.println("inner = " + inner);     // Now it is OK
      System.out.println("outer = " + outer);     // and outer is still here

      // All variable defined in the enclosing outer block still exist,
      // so you cannot redefine them here
      // int outer = 5;                           // Uncomment this for an error

    // Any variables declared in the previous inner block no longer exist
    // so you cannot refer to them
    // System.out.println("inner = " + inner);    // Uncomment this for an error

    // The previous variable, inner, does not exist so you can define a new one
    int inner = 3;
    System.out.println("inner = " + inner);       // ... and output its value
    System.out.println("outer = " + outer);       // outer is still around

As it stands, this program will produce the output:

inner = 2
outer = 1
inner = 3
outer = 1

If you uncomment any or all of the three statements as suggested, it won't compile:

javac Undefined variable: inner
         System.out.println("inner = " + inner);   // Uncomment this for an error
1 error

javac Variable 'outer' is already defined in this method.
         int outer = 5;                         // Uncomment this for an error
1 error

javac Undefined variable: inner
       System.out.println("inner = " + inner);    // Uncomment this for an error
1 error

How It Works

The method main() in this program has one block nested inside the block containing the code for the method. The variable outer is defined right at the start, so you can refer to this anywhere within the method main(), including inside any nested blocks. You are not allowed to re-declare a variable, so the commented statement that re-declares outer within the inner block will cause a compiler error, if you remove the double slash at the beginning of the line.

The variable inner is defined inside the nested block with the initial value 2, and you can refer to it anywhere from its declaration to the end of the inner block. After the closing brace of the inner block, the variable inner no longer exists, so the commented output statement that refers to inner is illegal. However, since the variable inner has expired, we can declare another one with the same name and with the initial value 3.

Note that all this is just to demonstrate the lifetime of local variables. It is not good practice to redefine variables that have expired, because of the potential for confusion. Also, although we have just used variables of type int in the example above, scoping rules apply to variables of any type.


There are other variables called class variables which have much longer lifetimes when they are declared in a particular way. The variables PI and E in the standard library class Math are examples of these. They hang around as long as your program is executing. There are also variables that form part of a class object called instance variables. We will learn more about these in Chapter 5.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor