| English | 释义 | 拼音 |
|---|---|---|
| static variable/ˈstætɪk ˈveərɪəbl/ | 静态变量 | jìng tài biàn liàng |
| static method/ˈstætɪk ˈmeθəd/ | 静态方法 | jìng tài fāng fǎ |
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; }
}
一个共享的静态 count
计数 是一个共享值,由类拥有。
一个静态(类)变量是……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
static 意味着由类拥有的一份共享副本。
如果构造函数做 count++ 且 计数 从 0 开始,创建 3 个对象后 计数 是多少?
难度 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.
在所示的 学生 类内部,静态方法可以使用 return score; 而无需指定任何 学生 对象。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
错误:score 是实例字段,且该静态方法没有隐式接收者。如果有一个非空的 Student s 参数,则在 学生 类内部 return s.score; 是有效的。
在 学生 类内部,哪个静态方法可以读取非空参数 s 的 score 值?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
显式引用 s 标识了具体对象;Student 类本身可以访问其私有字段 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.
哪个调用最能体现 getCount() 是 学生 类的静态方法?
难度 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。随后执行 Student alias = a; 和 alias.setScore(90);。请问 Student.getCount() 的值是多少?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
无论是赋值引用还是修改变量 score,都不会触发构造函数。count 保持为 2。
此构造函数计数器会在丢失对某个学生的引用时自动递减。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
只有修改 计数 的代码才会改变它的值。本示例统计的是构造次数,并未提供递减逻辑。