Part 05 / Topic 5 of 7
Scope
Scope is where a name can be seen, and how long it lives.
| Kind | Where it lives | How long |
|---|---|---|
| Parameter | Inside the method it belongs to | One method call |
| Local variable | Inside the block { } where it is declared |
Until that block ends |
Class-level constant (static final) |
The whole class | The whole program |
Think of a method call as a fresh sheet of paper: every time you call the method, Java hands it a brand-new sheet for its parameters and local variables. The sheet is thrown away the moment the method returns — nothing is remembered for next time.
static int addVisit() {
int visits = 0; // created fresh on every call
visits = visits + 1;
return visits;
} // visits is destroyed here, every time
System.out.println(addVisit()); // 1
System.out.println(addVisit()); // 1 again, NOT 2 — there is no shared memory between calls
This is a real difference from Python's module-level global variables: Java's static methods, as taught in this stage, have no shared mutable state at all unless you deliberately put it in a class-level field — which brings us to constants.
A class-level constant: the one shared, read-only value
A field declared static final at the top of the class is visible to every static method in the file, for the whole run of the program, and cannot be reassigned.
static final double GST_RATE = 0.18; // one copy, shared, read-only
static double priceWithGst(double amount) {
double tax = amount * GST_RATE; // "tax" is local; GST_RATE is read from the class
return amount + tax;
} // "tax" is gone here; GST_RATE is still there
System.out.println(priceWithGst(1000.0)); // 1180.0
System.out.println(priceWithGst(500.0)); // 590.0
static final is the closest Stage 1 gets to Python's module-level GST_RATE = 0.18: a shared value every method can read, but — unlike Python's global — Java gives you no keyword to reassign it from inside a method. If a value needs to change, pass it in and return a new one.
Block scope
Any { } — a for loop, an if, a plain block — starts a new, smaller scope. A variable declared inside one is gone once that block's } is reached.
static void blockScopeDemo(double price) {
for (int i = 0; i < 3; i++) {
double roundPrice = price - (i * 10); // only exists inside this loop's { }
System.out.println("Round " + i + ": " + roundPrice);
}
// Neither "i" nor "roundPrice" exists past this point.
if (price > 200) {
String note = "big order"; // only exists inside this if-block
System.out.println(note + " for ₹" + price);
}
// "note" cannot be used here.
}
blockScopeDemo(250.0);
// Round 0: 250.0
// Round 1: 240.0
// Round 2: 230.0
// big order for ₹250.0
Using a variable after its block has ended is a compile-time error, not a crash at run time — the compiler catches it before the program ever runs:
$ javac ScopeError.java
ScopeError.java:6: error: cannot find symbol
System.out.println(roundPrice);
^
symbol: variable roundPrice
location: class ScopeError
1 error
(from a scratch file that declares roundPrice inside a for loop and tries to print it after the loop — not part of examples/, because it is deliberately broken and never meant to compile as a finished program.)
🔍 Going deeper: Java also will not let you declare a second variable with the same name in a scope that already has one with that name — including a nested block reusing an outer method's variable name. Python's LEGB lookup (Part 5 of the Python course) does not exist in Java this way: Java resolves every name at compile time, strictly by which block it was declared in.
Full program: examples/04_scope_and_constants.java.
Done here? The practice set for Part 5 — including the solutions — is on the part page.