| English | 释义 | 拼音 |
|---|---|---|
| default constructor/dɪˈfɒlt kənˈstrʌktə/ | 默认构造函数 | mò rèn gòu zào hán shù |
A parameterised constructor does not supply a no-arg one
- Student declares only Student(String n). Creating new Student("Ada") works, but new Student() has no matching constructor.
- Java supplies an implicit default constructor only when none is declared. To allow both calls, explicitly write Student() and Student(String n).

Setting up a new object
- A constructor runs when an object is created with
new, to set its initial state. It has the same name as the class and no return type (not evenvoid). public Student(String n) { name = n; score = 0; }Its job: give the brand-new object sensible starting values.
Constructor parameters
- Parameters let each new object start with its own values.
new Student("Ada")passes"Ada"to the constructor'snparameter. - Inside,
name = n;copies the argument into the instance variable. Each successfulnewexpression creates a distinct object, even when its arguments are identical. Different arguments need not produce different field values if the constructor ignores or normalizes them.
两次new Student("Ada")调用指的是同一个对象,因为参数相等。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
每次成功的new表达式都会构建一个独立的对象;相等的参数并不意味着引用相等。
Overloading constructors
- A class can have several constructors with different parameter lists — overloading.
Student(String n)andStudent(String n, int s)are both valid. - Java picks the one whose parameters match your
newarguments. This offers callers flexible ways to build an object.
构造函数设置初始状态
每个新对象由构造函数的实参配置。
构造函数有……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
与类同名,无返回类型——连 void 都没有。
构造函数什么时候运行?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
新的 调用构造函数来构建对象。
如果你不写构造函数,Java 提供一个默认的无参构造函数。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
但写任何构造函数会移除那个免费默认。
一旦你定义了带参数的构造函数,new Student() 仍会自动工作。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
免费默认没了;需要就写一个无参构造函数。
写两个参数列表不同的构造函数是……的例子。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
同名、不同参数 = 重载。
The default constructor
- If you write no constructor, Java provides a hidden default constructor 默认构造函数 with no parameters. Fields first have default values (
0,false,null); explicit field initialisers can then set other initial values. - But once you write any constructor, that free default disappears. So if you need a no-arg constructor and a parameterized one, write both.
A constructor has the class's name and NO return type — writing void makes it an ordinary method, not a constructor. And once you define any constructor, Java stops giving you the free no-argument default; if you still want new Student(), you must write that constructor yourself.
Order the steps used to evaluate new Student("Ada").
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The supplied argument selects a matching constructor; declaring it does not also create a no-argument constructor.
Counter有一个私有int value = 7;以及一个显式构造器随后赋值value = 9;。构造后value的值是什么?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
字段初始化器分配 7,随后构造函数体分配 9。
Two constructors for Student:
public Student(String n) { name = n; score = 0; }public Student(String n, int s) { name = n; score = s; }new Student("Ada")→ score0;new Student("Bo", 75)→ score75.
Carry the reasoning to a new case
- Compare
class Counter { private int value = 7; }with an otherwise identical class whose explicit constructor assignsvalue = 9;. The first starts at 7 after its field initializer; the second ends construction at 9. - Fields receive default values before field initializers and the constructor body run. A programmer-written no-argument constructor is not the compiler-supplied default constructor, even though both take no arguments.
A constructor (same name as the class, no return type) runs on new to set an object's initial state, using parameters for per-object values. A class can overload several constructors. Write no constructor and Java supplies a default no-arg one — but defining any constructor removes that free default.