Scope and Access
| English | Chinese | Pinyin |
|---|---|---|
| scope | 作用域 | zuò yòng yù |
| local variable | 局部变量 | jú bù biàn liàng |
| shadows | 遮蔽 | zhē bì |
Where a variable can be used
- The scope 作用域 of a variable is the region of code where it can be used.
- A variable exists only inside the
{ }block where it's declared. - Outside its scope, the name is unknown — using it is a compile error.
- Scope keeps names from clashing and limits where a variable matters.
Local variables
- A local variable 局部变量 is declared inside a method (or block).
- It exists only while that method runs, then it's gone.
- Each call gets a fresh local variable; they don't persist between calls.
- Use locals for temporary work inside a method.
Instance variables live longer
- An instance variable belongs to the object and lives as long as the object does.
- It's visible to every method in the class.
- Locals are temporary and method-private; instance variables are the object's lasting state.
- Choose an instance variable for state the object must remember; a local for scratch work.
Shadowing
- If a parameter (or local) has the same name as an instance variable, it shadows 遮蔽 it.
- Inside that method, the name refers to the parameter, hiding the instance variable.
void setName(String name) { name = name; }just assigns the parameter to itself — a bug!- To reach the shadowed instance variable, use
this(next lesson).
Where can you use this variable?
Scope decides where a name is visible. Sort each case.
A local variable exists only while its method runs and is invisible outside it. Don't expect a value stored in a local to survive to the next call — use an instance variable for lasting state. And beware shadowing: a parameter with the same name as an instance variable hides it, so name = name; assigns the parameter to itself and never touches the field.
Scope and shadowing:
- Instance variable:
private String name; void setName(String name) { name = name; }→ wrong: assigns the parameter to itself.- The instance variable
nameis untouched — the parameter shadows it.
Scope is where a variable can be used. A local variable (declared in a method) exists only while the method runs; an instance variable belongs to the object and lasts as long as it does, visible to all its methods. A parameter with an instance variable's name shadows it — reach the field with this.
The scope of a variable is...
Scope is where the name is valid.
A local variable exists...
Locals are created on each call and vanish when the method ends.
Which kind of variable should hold state the object must remember between method calls?
Instance variables persist for the object's lifetime.
A parameter with the same name as an instance variable shadows (hides) that instance variable inside the method.
Inside the method, the name means the parameter; use this to reach the field.
When a parameter hides an instance variable of the same name, it is said to ___ it (one word).
Shadowing hides the field behind the local name.