Scope and Lifetime of Variables

Naveen AnamaneniUpdated · 5 min read

A hotel room key works only for your room, and only until you check out. Your name on the guest register lasts longer: the whole stay. Variables are similar. Scope is where in the code a variable's name works. Lifetime is how long the variable actually exists while the program runs. Most of the time the rule is simple: a variable belongs to the curly braces it was declared in.

The Rule of the Braces

A block is any code between { and }. A local variable can be used from the line where it's declared to the end of its block, including any blocks inside it. After the closing brace, its name no longer exists.

Variables appear when declared and disappear at their closing brace
Journey.java▶ Try it
public class Journey {
    public static void main(String[] args) {
        String city = "Mysuru";
        if (city.length() > 3) {
            int fare = 350;
            System.out.println(city + " fare: Rs. " + fare);
        }
        // fare no longer exists here
 
        for (int i = 1; i <= 2; i++) {
            System.out.println("Stop " + i + " on the way to " + city);
        }
        // i no longer exists here
    }
}
Output
Mysuru fare: Rs. 350
Stop 1 on the way to Mysuru
Stop 2 on the way to Mysuru

How it works

  1. Line 3: city is declared directly in main, so it can be used anywhere in main after this line, including inside the if and the loop.
  2. Line 5: fare is declared inside the if block. It exists only until the } on line 7.
  3. Line 10: i is declared in the for loop header. It exists only inside the loop.

Using fare after line 7 gives the same error you'd get for a misspelled name, because outside its block the name simply doesn't exist:

Compiler output
Journey.java:7: error: cannot find symbol
        System.out.println(fare);
                           ^
  symbol:   variable fare
  location: class Journey

Try it yourself: move the line int fare = 350; above the if. Can you now print fare after the if block?

Scope of Each Kind of Variable

KindScope: where the name worksLifetime: how long it exists
Block variableFrom its declaration to the end of its blockUntil the block finishes
Loop variable (for (int i ...))Inside the loop onlyUntil the loop ends
Method parameterThe whole method bodyOne method call
Instance variableThe whole class (every non-static method)As long as its object exists
Static variableThe whole class, and other classes through ClassName.nameAs long as the class is loaded

Shadowing: Same Name, Inner Scope

A local variable or parameter may have the same name as a field. Inside that method, the local one hides (shadows) the field:

Shadow.java▶ Try it
public class Shadow {
    static String city = "Delhi";
 
    public static void main(String[] args) {
        String city = "Kochi";
        System.out.println("Local city: " + city);
        System.out.println("Field city: " + Shadow.city);
    }
}
Output
Local city: Kochi
Field city: Delhi

Line 6 uses the nearest city, the local one. Line 7 reaches the field by writing the class name in front. With instance variables, you'll write this.city instead, as Level 2 shows.

Two local variables with the same name in overlapping scopes are not allowed: declaring int fare inside the if when main already has a fare gives variable fare is already defined.

Common Mistakes

Declaring inside a block and using it after. If you need a value after an if or loop, declare the variable before the block and assign it inside.

Expecting a loop variable to keep its last value. for (int i = 0; ...) makes i disappear when the loop ends. Declare int i; before the loop if you need it later.

Confusing a shadowing local with the field. Changing the local doesn't change the field, which can look like a bug that "ignores" your assignment.

Practice

  1. Easy: In Journey, try to print i after the loop. Read the error, then fix it by declaring i before the loop.
  2. Harder: Write a program that works out a total fare inside an if (only when the passenger count is above 0) and prints the total after the if. Where must the variable be declared?

Quick check: Scope and lifetime

4 questions
1Where can a variable declared inside an if block be used?
2What happens to i in for (int i = 0; i < 3; i++) after the loop?
3A method has a local city and the class has a static field city. Which does city mean inside the method?
4How long does an instance variable live?

Key Takeaways

Was this tutorial helpful?

Lesson 32 of 482