Scope and Access · Escopo e Acesso
| English | Português |
|---|---|
| local variable/ˈləʊkl ˈveərɪəbl/ | variável local |
| scope/skəʊp/ | escopo |
| shadows/ˈʃædəʊz/ | sombras |
A local resets; a field remembers
- A method creates
int count = 0;, increments it and returns 1. Calling that method twice returns 1 both times because each invocation has a fresh local variable. - A method that increments an object's field instead returns 1 and then 2 for the same initially zero-valued object. Distinguish where a name is usable from how long the stored state is retained.
Scope concerns a name in source code
- The · A scope 作用域 of a declaration is the part of the program where its name can be used. A block local is in scope from its declaration through the remaining block; a for-loop variable is not available after that loop.
- Scope, access permission and lifetime answer different questions. A name may be out of scope even though an object it once referenced remains reachable through another reference; fields are not governed by the same declaration rule as block locals.
Initialize local variables before reading them
- A local variable 局部变量 is declared in a method or block. Java requires it to be definitely assigned before its value is read;
int x; return x;is a compile-time error, not an automatic return of zero. - Instance fields receive default values during construction before initializers and constructor code run. In the example, count starts at 0; that field belongs to each object and persists between its method calls.
public class ScopeCounter {
private int count;
public int localCount() {
int count = 0;
count++;
return count;
}
public int rememberedCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
public static int readCount(ScopeCounter other) {
return other.count;
}
}
The local declaration int x; automatically gives x the value zero, so return x; can compile.
Locals must be definitely assigned before reading; field defaults do not apply to local variables.
A parameter can hide a field name
- The parameter
countinsetCount(int count)shadows 遮蔽 the field for unqualified uses of that name.count = count;would assign the parameter to itself;this.count = count;updates the receiver's field. - A local may shadow a field too, as localCount demonstrates. Do not infer that every duplicate local declaration is allowed: redeclaring an already in-scope local in a nested block of the same method is a compile-time error.
The scope of a variable is...
Scope is where the name is valid.
After an ordinary method invocation finishes, can the caller use a local variable declared only inside that method by its local name?
The local name is not in the caller's scope. An object referenced by that local can nevertheless be returned and remain reachable.
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.
Access depends on the declaring class
- Code inside ScopeCounter may read another ScopeCounter object's private count via an explicit reference. The shown static readCount method has no
this, butother.countis valid for a non-null parameter. - An unrelated external class cannot directly read that private field. Private access is not restricted to “only my own object”; an instance method has an implicit receiver, while a static method needs an explicit reference to access instance state.
Do not combine name scope, default initialization and private access into one rule. A local does not automatically start at zero, an instance field cannot be read unqualified from a static method, and a private field of another instance may be accessed inside its declaring class.
Scope, field state and access permission · Onde você pode usar esta variável?
Distinguish declarations from access context; another instance is not automatically inaccessible.
The shown static readCount method may read other.count when other is non-null.
It uses an explicit object reference and is inside the class that declares the private field; no implicit this is needed.
Predict a new receiver and a returned object
- After two rememberedCount calls on a, its field is 2; a newly constructed b still has count 0. Calling a.localCount() returns 1 without resetting a's field, because its local count shadows that field.
- A method can construct an object in a local variable and return its reference. The local name is then unavailable to the caller, but the returned object can remain reachable; the end of a name's scope does not destroy that object.
Scope tells where a name is usable; access modifiers control permission; object state can outlive a local reference. Initialize locals before reading them, use fields for per-object remembered state, and distinguish shadowing from access to another object's private field.
For a newly constructed ScopeCounter a, call a.rememberedCount() twice, then a.localCount(). What does a.localCount() return?
localCount has a fresh local count initialized to zero; it increments and returns 1 without changing the field.