| English | 의미 |
|---|---|
| local variable/ˈləʊkl ˈveərɪəbl/ | 로컬 변수 |
| scope/skəʊp/ | 범위 |
| shadows/ˈʃædəʊz/ | 그림자 |
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에게 0의 값을 부여하므로 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 코인 + 오늘 첫 완료 시 소액 보너스
로컬 이름은 호출자의 범위(scope)에 포함되지 않습니다. 그러나 해당 로컬이 참조하는 객체는 반환될 수 있으며 여전히 접근 가능합니다.
메서드 간에 객체가 기억해야 할 상태를 유지하기 위해 어떤 유형의 변수를 사용해야 합니까?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
인스턴스 변수는 객체의 수명 동안 지속됩니다.
인스턴스 변수와 동일한 이름의 파라미터는该方法 내부에서 해당 인스턴스 변수를 가림(shadows/hides).
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
메서드 내부에서는 이름이 파라미터를 의미하며, 필드에 접근하려면 this를 사용해야 합니다.
동일한 이름의 인스턴스 변수를 가릴 때 파라미터는 이를 ___한다고 합니다 (단어 하나).
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
가림(Shadowing)은 로컬 이름 뒤에 필드를 숨깁니다.
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가 null이 아닐 때 other.count 값을 읽을 수 있습니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
명시적인 객체 참조를 사용하며, 이는 비공유(private) 필드를 선언한 클래스 내부에 위치하므로 명시적 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는 0으로 초기화된 새로운 로컬 카운트를 가지며, 이 카운트를 증가시켜 1을 반환하지만 필드의 값은 변경하지 않습니다.