| English | 释义 | 拼音 |
|---|---|---|
| local variable/ˈləʊkl ˈveərɪəbl/ | 局部变量 | jú bù biàn liàng |
| scope/skəʊp/ | 作用域 | zuò yòng yù |
| shadows/ˈʃædəʊz/ | 遮蔽 | zhē bì |
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 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;
}
}
局部声明 int x; 会自动赋予 x 值为零,因此 return x; 可以编译通过。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
局部变量在读取前必须已确定赋值;字段的默认值不适用于局部变量。
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.
变量的作用域是...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
作用域是名称有效的地方。
普通方法调用结束后,调用方能否通过局部变量名访问仅在该方法内部声明的局部变量?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
该局部名称不在调用方的作用域中。但通过该局部变量引用的对象仍可被返回并继续保持可达。
哪种类型的变量应该持有对象必须在方法调用之间记住的状态?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
实例变量贯穿对象的整个生命周期。
如果参数与实例变量同名,则它会遮蔽(隐藏)该方法内的实例变量。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
在方法内部,该名称指代参数;使用此语法可访问字段。
当参数遮蔽了同名的实例变量时,这被称为___它(一个单词)。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
遮蔽通过局部名称将字段隐藏起来。
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.
作用域、字段状态与访问权限
需区分声明与访问上下文;另一个实例并非自动不可访问。
所示的静态 readCount 方法可能在 相反 非空时读取 other.count。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
它使用显式的对象引用,并且位于声明该私有字段的类内部;不需要隐式的 this。
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.
对于一个新构建的 ScopeCounter a,调用两次 a.rememberedCount(),然后调用 a.localCount()。a.localCount() 返回什么?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
localCount 是一个初始化为零的新局部计数器;它递增并返回 1,而不改变该字段。