Java Scope, Code Blocks and the Call Stack
Braces make rooms: a variable is born at its line and dies at its closing brace. Then the call stack, frame by frame, as main calls orderTotal, discountFor and percent to turn 250 into a bill of 225.
What you will learn
- Explain block scope: a variable lives from its line to the closing brace of its block
- Fix 'cannot find symbol' errors caused by using a variable outside its block
- Tell when a local may share a name with a static field and when it may not
- Trace how the call stack pushes and pops a frame for each method call
Before you start
- Java methods and overloading
Where does a variable live, when does it die, and how does Java keep track when methods call other methods? Two ideas answer all three: block scope and the call stack.
Braces make rooms
Every pair of curly braces is a room: a class, a method, an if block, a loop. A variable declared inside a room is born and dies with it. Think of a house: from the bedroom you can't see how much sugar is left in the kitchen jar.
From an inner room you can see outwards, but not the other way round.
package org.studyeasy.s02l14;
public class Ex01_IfBlockScope {
static int finalAmount(int count) {
int subtotal = 250;
if (count >= 4) {
int off = subtotal / 10;
subtotal = subtotal - off;
}
return subtotal;
}
public static void main(String[] args) {
System.out.println("4 items: " + finalAmount(4));
System.out.println("2 items: " + finalAmount(2));
}
}
4 items: 225
2 items: 250
The if block can use subtotal, because it looks outwards into the method. off is born inside the if and dies at its closing brace. The return line sits outside, so it can't see off. Add System.out.println(off); after the if and javac says "cannot find symbol". The fix: return the value you need, or declare the variable outside the block.
Born at the line, gone at the brace
| Variable | Born | Dies |
|---|---|---|
count (parameter) | with the call | when the method returns |
subtotal | int subtotal = 250; | the method's closing brace |
off | int off = ... inside the if | the if's closing brace |
A loop has its own room
package org.studyeasy.s02l14;
public class Ex02_LoopScope {
public static void main(String[] args) {
for (int cup = 1; cup <= 3; cup++) {
System.out.println("Pouring cup " + cup);
}
System.out.println("Done pouring");
}
}
Pouring cup 1
Pouring cup 2
Pouring cup 3
Done pouring
cup is declared in the loop header, so it belongs to the for block, not to main. Print it after the loop and you get "cannot find symbol" again.
Cheat sheet
| Kind | Lives |
|---|---|
| Method parameter | the whole method, until it returns |
| Local in a method | its line to the method's closing brace |
| Local in an if / else | its line to that block's closing brace |
| for-loop variable | the whole loop |
| static field | with the class, until the program ends |
The rule is always the same: find the nearest pair of braces around the declaration.
One name, two places
package org.studyeasy.s02l14;
public class Ex03_Shadowing {
static int discount = 10;
public static void main(String[] args) {
int discount = 20;
System.out.println("local discount: " + discount);
System.out.println("class discount: " + Ex03_Shadowing.discount);
}
}
local discount: 20
class discount: 10
The static field belongs to the class, a house of its own in memory. The local is an apartment inside main. Plain discount means the nearest one, 20. The class name reaches the field: Ex03_Shadowing.discount is still 10. This is called shadowing.
Two locals with the same name are not allowed, though. Add a second int discount = 5; inside main and javac says "variable discount is already defined".
The call stack: push and pop
package org.studyeasy.s02l14;
public class Ex04_CallStack {
public static void main(String[] args) {
int bill = orderTotal(250);
System.out.println("Bill: " + bill);
}
static int orderTotal(int subtotal) {
int off = discountFor(subtotal);
System.out.println("Discount: " + off);
return subtotal - off;
}
static int discountFor(int subtotal) {
return percent(subtotal, 10);
}
static int percent(int amount, int rate) {
return amount * rate / 100;
}
}
Discount: 25
Bill: 225
Java tracks calls with a stack, like a pile of plates. Each call puts a new plate, a frame, on top. The frame holds that call's own parameters and locals.
mainstarts, so its frame is pushed.billhas no value yet.maincallsorderTotal(250): a new frame withsubtotal250.orderTotalcallsdiscountFor, which callspercent(250, 10). Four frames now,percenton top.percentworks out 250 x 10 / 100 = 25. Its frame pops and 25 goes down;discountForreturns it too, sooffbecomes 25 and "Discount: 25" prints.orderTotalreturns 250 - 25 = 225 and pops.billis 225, "Bill: 225" prints, and whenmainends the stack is empty.
Make percent call itself (return percent(amount, rate);) and frames pile up until the program stops with StackOverflowError. You'll also see these frames in IntelliJ's debugger, covered in debugging in IntelliJ.
Recap
- Every pair of braces is a room. An inner room sees outwards, never inwards.
- A variable lives from its declaration to the closing brace of its block.
- "cannot find symbol" often means you used a variable outside its room.
- A local can shadow a static field; two locals can't share a name.
- Each call pushes a frame; the top one finishes first and passes its answer down.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
