Scope

index
BASIC

Every variable has a scope, which is the part of the program in which the variable may be used. In some cases, it is possible to access variables that are "out of scope."

Variables declared in classes

Variables that are declared within a class are available everywhere within the class. However, if they are declared with an initial value, that value may not be used in earlier declarations.

public class MyClass {
    int x = 5;
    int y = x + 1; // this declaration can use the value of "x"
    int z = w + 1; // illegal because "w" hasn't been defined yet
    ...
    // "x", "y", "z", and "w" are available in methods everywhere
    // but the value of "w" can't be used in declarations yet
    ...
    int w = 3;
    // now the value of "w" can be used in declarations
    ... 
}

Here's one way to think of it. When a class is first used, Java goes through and executes all the top-level declarations (that is, those not in methods) first, in the order that they appear. After that, the methods in the class are available for use.

Exception: Static methods in a class (such as the main method) can only use static variables in that class.

Variables declared in methods

A method consists of a header and a body; the body is a block.

public int add(int x, int y) { // header and start of block
    int sum;
    sum = x + y;
    return sum;
} // end of block

The parameters (in this example, x and y) are available throughout the method, and only within the method. Variables declared within the body follow the scope rules for blocks (see below).

The parameters and the variables declared within the body are called local variables; they are created when the method is invoked (used), and discarded when the method returns. Local variables do not retain their values from one invocation (use) of the method to the next; each invocation starts all over again.

Variables declared in blocks

If you declare a variable within a block, that variable is accessible (can be used) from the point you declare it to the end of the block. For example:

{
    x = 1; // illegal!
    int x; // scope of "x" starts here
    x = 2; // legal
} // scope of "x" ends here
x = 3; // illegal!

You should normally put your variable declarations first in a block.

Variables declared in statements

Most control statements simply control a block, and the scope rules are those for blocks (above). However, there are two special cases.

In a for loop, you may declare one or more variables in the initialization part, and give them values. These variables are available throughout the entire for loop.

for (int i = 1, j = 100; i < j; i = 2 * i, j--) {
    System.out.println("i = " + i + ", j = " + j); 
}

The do-while loop is actually not a special case, but it needs special care, because the test isn't inside the block. Hence, variables declared within the block are not available in the test.

do {
    System.out.print("Password? ");
    String input = scanner.nextLine().trim();
} while (!input.equals(PASSWORD)); // illegal! "input" is out of scope

Shadowing

INTERMEDIATE

Declarations in a block may shadow other declarations.