Java Scope

Java scope means the part of a program where a variable can be used. A variable is only available inside the method, block, or class area where it is declared.

Understanding scope helps you avoid name errors and keeps each variable limited to the code that actually needs it.

Local Variables

A variable declared inside a method is called a local variable. It belongs to that method and cannot be used from another method.

public class Main {
    static void printMessage() {
        String message = "Hello from the method";
        System.out.println(message);
    }

    public static void main(String[] args) {
        printMessage();
    }
}

Output:

Hello from the method

The variable message is declared inside printMessage, so it can only be used inside that method. Code in main can call the method, but it cannot directly use message.

Method Parameter Scope

Method parameters also have local scope. A parameter exists only while its method is running.

public class Main {
    static void greet(String name) {
        System.out.println("Hello, " + name);
    }

    public static void main(String[] args) {
        greet("Ava");
        greet("Leo");
    }
}

Output:

Hello, Ava
Hello, Leo

The parameter name is created each time greet is called. After that call finishes, that parameter is no longer available.

Block Scope

A block is code between curly braces, such as the body of an if statement or a loop. Variables declared inside a block can only be used inside that block.

public class Main {
    public static void main(String[] args) {
        int score = 85;

        if (score >= 70) {
            String result = "pass";
            System.out.println(result);
        }

        System.out.println(score);
    }
}

Output:

pass
85

The variable result is declared inside the if block, so it can only be printed inside that block. The variable score is declared in main, so it can be used throughout the rest of main.

Loop Variable Scope

When a variable is declared in a for loop header, it belongs to the loop. It cannot be used after the loop ends.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            System.out.println("Round " + i);
        }

        System.out.println("Done");
    }
}

Output:

Round 1
Round 2
Round 3
Done

The variable i exists only inside the for loop. If you need a value after the loop, declare a separate variable before the loop begins.

Class Fields

A variable declared directly inside a class, but outside any method, is called a field. A static field can be used by static methods in the same class.

public class Main {
    static String course = "Java";

    static void printCourse() {
        System.out.println(course);
    }

    public static void main(String[] args) {
        printCourse();
        System.out.println(course + " scope");
    }
}

Output:

Java
Java scope

The field course is available to both printCourse and main because both methods are inside the same class. Fields are useful when several methods need the same shared value, but local variables are usually better for temporary values.

Common Mistakes

  • Trying to use a local variable from another method.
  • Trying to use a variable after the block where it was declared has ended.
  • Expecting a for loop variable to still exist after the loop.
  • Using a field when a local variable would keep the code simpler.

Takeaway: a Java variable can only be used inside its scope, so declare variables in the smallest area where they are needed.