| English | 意味 |
|---|---|
| static variable/ˈstætɪk ˈveərɪəbl/ | 静的変数 |
| static method/ˈstætɪk ˈmeθəd/ | スタティックメソッド |
Two objects, one construction count
- Ada and Bo have separate scores, but creating either student increases the same static variable 静态变量,
Student.count. After two constructions, the count is 2; changing Ada's score does not change Bo's score. - Compare what each value describes: a score describes one student, while this counter describes constructions of the class. A copied reference to Ada creates no new object and does not increment the counter.
Put each field at the right level
- An instance field has a separate storage location in each object. A static field has one storage location for this loaded class, even before any objects are constructed;
privatecontrols access, not the number of copies. - In this small single-threaded example, the constructor increases
countonce per construction. It counts objects constructed, not objects still alive: losing a reference does not automatically decrease it.
class Student {
private int score;
private static int count = 0;
public Student(int initialScore) {
score = initialScore;
count++;
}
public int getScore() { return score; }
public void setScore(int newScore) { score = newScore; }
public static int getCount() { return count; }
public static int readScore(Student s) { return s.score; }
}
1つの共有される静的カウンタ
count は1つの共有値であり、クラスによって所有されます。
静的(クラス)変数は...
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
static は、クラスによって所有される1つの共有コピーを意味します。
コンストラクタで count++ が行われ、count の初期値が 0 の場合、3つのオブジェクトを作成した後の count の値は何になりますか?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
各生成で共有カウンタが増加するため → 3.
A static method has no receiver
- A static method 静态方法 has no implicit current object, so it cannot use
thisor an unqualified instance field such asscore.getCount()can readcountbecause that field belongs to the class. - Static does not forbid working with objects.
readScore(Student s)receives an explicit object reference and readss.score; it is insideStudent, where access to that private field is allowed. Heresmust be non-null.
return score; inside a static method is a compile-time error in this class. return s.score; inside the shown static method is valid: the reference identifies which student's score to read.
表示されたStudentクラスの内部では、特定のStudentオブジェクトを明示せずにreturn score; を使用して静的メソッドを実行できます。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
誤り:scoreはインスタンスフィールドであり、この静的メソッドには暗黙のレシーバーが存在しません。nullでないStudent sパラメータがある場合、Studentの内部でreturn s.score; は有効です。
Studentの内部で、nullでないパラメータsのスコアを読み取れる静的メソッドはどれか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
明示的な参照sが対象オブジェクトを特定するため、Student自体はそのprivate scoreフィールドにアクセスできます。
Trace class and object calls
- After
Student a = new Student(50);andStudent b = new Student(75);,Student.getCount()is 2,a.getScore()is 50 andb.getScore()is 75.a.setScore(60)changes only Ada's score. Student.readScore(a)then returns 60 without constructing another student. The method is static, yet its explicit parameter identifies an existing object whose state it reads.
getScore() のようなインスタンスメソッドは...に対して呼び出されます。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
インスタンスメンバーは特定のオブジェクトに対して動作します。
Separate legal syntax from clear style
- Prefer
Student.getCount()to show that the method belongs to the class. Java also permitsa.getCount()for this static method; that syntax does not make the count specific to Ada. a.getScore()is an instance call with Ada as its receiver.Student.getScore()is invalid here because the method needs an object and is not static; choose field and method kinds from their responsibilities.
countGet()がStudentの静的メソッドであることを最もよく示している呼び出しはどれか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
Student.getCount()ではクラス名が明確に指定されています。Javaでは、静的メソッドに対してstudent.getCount()と記述することも可能ですが、この表記法はクラスレベルでの意味を曖昧にします。
Predict a reference copy
- Starting with the two constructed students,
Student alias = a;leaves the construction count at 2.alias.setScore(90)changes Ada's existing object, soa.getScore()is 90 and Bo's score remains 75. - Count executions of
new Student(...), then trace each object's state separately. Neither an accessor call nor a reference assignment creates a student, and a static method may still read an explicitly supplied object.
Static fields store class state; instance fields store object state. Static methods have no this, but may use explicit object references. Prefer class-qualified static calls and distinguish construction counts from live-object counts.
2人の学生が生成されたため、countは2です。次にStudent alias = a; およびalias.setScore(90); を実行します。Student.getCount()の結果は何ですか?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
参照の割り当てやスコアの更新はコンストラクタを実行しません。countは2のままです。
このコンストラクタのカウンターは、学生への参照が失われると自動的に減少します。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
countを変更するコードのみがその値を変化させます。この例では生成回数をカウントし、減少処理を提供していません。