Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Scope

Every variable can be used only in a certain part of the program. That part is called its scope. The rule in Java is simple: a variable exists from the line where it is declared to the end of the block (the curly brackets) that contains it. Knowing this rule explains many "cannot find symbol" errors.

Method scope

A variable declared in a method is a local variable. It exists only inside that method. Another method cannot see it, even if it has the same name:

public class Main {
    static void first() {
        int count = 1;
        System.out.println("first: " + count);
    }

    static void second() {
        int count = 99; // a different variable with the same name
        System.out.println("second: " + count);
    }

    public static void main(String[] args) {
        first();
        second();
    }
}
Output
first: 1
second: 99

Parameters are local variables too: they exist only while the method runs.

Block scope

A variable declared inside an if, a loop or any other block disappears at the end of that block. The loop variable of a for loop belongs to the loop:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            int square = i * i;
            System.out.println(square);
        }
        System.out.println(i);
    }
}
The compiler says
Main.java:7: error: cannot find symbol

The last println cannot see i, so the program does not compile. If you need a value after the loop, declare the variable before it:

public class Main {
    public static void main(String[] args) {
        int found = -1;
        int[] values = {4, 8, 15, 16, 23};
        for (int i = 0; i < values.length; i++) {
            if (values[i] > 10) {
                found = i;
                break;
            }
        }
        System.out.println("First value above 10 is at index " + found);
    }
}
Output
First value above 10 is at index 2

Class-level variables

A variable declared inside the class but outside every method belongs to the class. With static it can be used by all the static methods of the class. Such variables are called fields; the Class Attributes topic covers them fully.

public class Main {
    static int visits = 0;

    static void visit() {
        visits++;
    }

    public static void main(String[] args) {
        visit();
        visit();
        visit();
        System.out.println("Visits: " + visits);
    }
}
Output
Visits: 3

A field keeps its value between method calls, while a local variable starts fresh every time the method runs.

Shadowing

If a local variable has the same name as a field, the local one wins inside its method. The field is shadowed:

public class Main {
    static int x = 10;

    public static void main(String[] args) {
        int x = 5;
        System.out.println(x);
        System.out.println(Main.x);
    }
}
Output
5
10

Main.x still reaches the field. Shadowing is legal but confusing, so give variables different names. In classes you will meet the keyword this, which solves the same problem for constructor parameters.

Practice exercises

  1. Declare a variable inside an if block and try to print it after the block. Read the error, then fix it.
  2. Write a method that counts how many times it has been called, using a static field.
  3. Find the last even number in an array, keeping the result in a variable declared before the loop.