Skip to lesson

Local variables and scope

A local variable is a variable made inside a method. It exists only inside that method, and no other method can see it.

Each method has its own variables

printName() tries to print the name made in main, and Java says cannot find symbol. A variable made inside a method is a local variable: only that method can see it.

Java
// in main:String name = "Divya";printName();
static void printName() {    System.out.println(name);   // error: cannot find symbol}

To give the method the value, pass it as an argument: printName(String name) and the call printName(name);. Parameters are local variables of their method too.

Same name, two variables

Both main and showOther declare a variable called marks. They do not clash: each method has its own local marks, and changing one never touches the other.

Example 1 · Same name, two different variables

TwoMarks.javaJava
public class TwoMarks {    public static void main(String[] args) {        int marks = 80;        showOther();        System.out.println("main's marks: " + marks);    }
    static void showOther() {        int marks = 50;        System.out.println("showOther's marks: " + marks);    }}
Output
showOther's marks: 50
main's marks: 80

What this code does

  • int marks = 50; in showOther makes a second, separate variable. It is not an error, because each method has its own names.
  • When showOther ends, its marks is gone, and main still sees its own 80.

Scope: where a variable can be used

You make result inside an if, print it after the }, and get cannot find symbol again. A variable's scope runs from its declaration to the } that closes its block, like the counter in The for loop (opens in a new tab).

Java
int marks = 80;if (marks >= 35) {    String result = "Pass";    System.out.println(result);}System.out.println(result);   // error: cannot find symbol

To use a value after the block, declare the variable before the block, with a starting value: String result = "Fail";.

A local variable gets no starting value of its own. int total; total = total + 5; gives variable total might not have been initialized; write int total = 0;.

A fresh start on every call

countVisit() adds 1 to count on every call, yet it always prints 1. Each call gets a fresh frame, a piece of memory for its local variables, and the frame is thrown away when the method ends.

Trace the code

Two calls, two fresh frames

public class FreshStart {
public static void main(String[] args) {
countVisit();
countVisit();
}
static void countVisit() {
int count = 0;
count++;
System.out.println("Visits: " + count);
}
}

Variables

None yet.

Output

(nothing printed yet)

Line 3main makes the first call. Java gives countVisit a fresh frame for its variables.

Step 1 of 10
To keep a count across calls, keep the variable in main and let the method return the new value: visits = addVisit(visits);.

The call stack

main calls first, and first calls second. Which one finishes first? Frames pile up like plates in the call stack: a new call goes on top, and the top one finishes first.

Three moments of the call stack. While main runs, only main is on it. When main calls first, first goes on top. When first calls second, second goes on top of both, and it is the first to finish and be removed.

Example 2 · The last method called finishes first

StackOrder.javaJava
public class StackOrder {    public static void main(String[] args) {        first();        System.out.println("main ends");    }
    static void first() {        System.out.println("first starts");        second();        System.out.println("first ends");    }
    static void second() {        System.out.println("  second runs");    }}
Output
first starts
  second runs
first ends
main ends

What this code does

  • The stack grows: main is at the bottom, first goes on top of it, then second on top of first.
  • second ends first and comes off the top; then first can print first ends; main ends last.

Next step

Check your understanding

8 quick questions