Local variables and scope
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
// 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
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); }}showOther's marks: 50 main's marks: 80
What this code does
int marks = 50;inshowOthermakes a second, separate variable. It is not an error, because each method has its own names.- When
showOtherends, itsmarksis gone, andmainstill 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).
int marks = 80;if (marks >= 35) { String result = "Pass"; System.out.println(result);}System.out.println(result); // error: cannot find symbolTo use a value after the block, declare the variable before the block, with a starting value: String result = "Fail";.
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
Variables
None yet.
Output
(nothing printed yet)Line 3main makes the first call. Java gives countVisit a fresh frame for its variables.
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.
Example 2 · The last method called finishes first
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"); }}first starts second runs first ends main ends
What this code does
- The stack grows:
mainis at the bottom,firstgoes on top of it, thensecondon top offirst. secondends first and comes off the top; thenfirstcan printfirst ends;mainends last.
Next step
Check your understanding
8 quick questions