Lesson · Java Language Fundamentals
Variables and scope: who can see what, and when
13 min readVariables & Scope
Four kinds of variables, one rule for visibility (lexical scope), one rule for values (definite assignment), and the version of var the compiler actually accepts.
The four kinds, and who gets default values
Every variable in Java is one of four kinds, and the kind decides two things: where it lives, and whether something initialises it for you.
- Local: declared inside a method or block. Lives on the stack, dies at the end of the block, and has NO default value. Read it before assigning and the code does not compile.
- Parameter: a local whose value arrives from the caller. Same no-default rule, and you may reassign it freely inside the method.
- Instance field: declared in a class, one per object. Created and defaulted when the object is constructed: 0, 0.0, false, the null character, or null.
- Static field: one per class, defaulted the same way when the class first initialises. Shared by every instance and every thread.
Scope is lexical: the braces in the source decide
A variable is visible from its declaration to the end of the block that contains it. Nested blocks see everything around them; the reverse is never true. Because that rule reads straight off the source, the compiler can verify all of it before a single instruction runs.
Every construct with a block creates a scope: if, for, while, try, a lambda body, even a bare { }. Loop variables are recreated per iteration, and the enhanced-for variable is fresh each time round. That detail becomes visible the moment you capture the variable in a lambda.
Scope at each line
Variables in scope
nothing yet
A caret walks through the code; the panel lists every variable visible at that line. Watch the field get shadowed, the block variable appear and vanish, and this reach past the local.
Shadowing: allowed for fields, forbidden for locals
When names collide, Java's rules are narrower than C++'s. A local or parameter may shadow a field, and the field is still there: this.name reaches it. But two locals cannot share a name in nested blocks. Declaring int x inside a block while an outer x is in scope is a compile error, full stop. Java traded that flexibility for a name-resolution model you can explain in one sentence.
The habits that follow: give fields and locals different names when you can, reach for this. when you cannot, and treat a shadowed field as a reading hazard rather than a feature.
var: inference with rules
var is not dynamic typing. It asks the compiler to infer a type from the initialiser, and that type is then fixed forever. Since the type comes from the initialiser, every rule follows from requiring one.
- Allowed: locals with an initialiser (
var list = new ArrayList<String>();), for-loop and for-each headers, try-with-resources, lambda parameters. - Not allowed: fields, method parameters, return types (no initialiser to read);
var x = null;andvar x = {1, 2};(nothing to infer from). - Never changes type:
var x = 1; x = "s";does not compile, which is exactly what static typing means.
Locals have no defaults
Use a local before assigning it and compilation stops with 'variable might not have been initialized'. Fields do the opposite: they quietly start at 0, false or null, so an unset field surfaces later as a NullPointerException or a suspicious zero. Same language, two different promises, and the strict one is on your side.
Interview angles
What is definite assignment?+
The compiler proves, on every possible path, that a local variable was assigned before its first read. Fields are exempt because they are defaulted at construction. The rule is why 'possibly uninitialised' errors appear at compile time instead of surprising you at runtime.
Can two locals share a name in nested blocks?+
No. Java forbids a local from shadowing another local, unlike C++. Fields are different: a local or parameter may shadow a field, and this.field still reaches the original. The restriction keeps name resolution to one readable rule.
Where is var legal and where is it not?+
Legal where an initialiser exists and a local is involved: local declarations, for headers, for-each, try-with-resources, lambda parameters. Illegal for fields, parameters, return types, null initialisers and array initialisers. The inferred type is then immutable.
How do default values differ between locals and fields?+
Fields are defaulted by construction: 0, 0.0, false, the null character and null, per type. Locals and parameters get nothing, and the compiler enforces assignment before use. Relying on field defaults is legal but often hides missing initialisation.