Constructors · Constructores
| English | Español |
|---|---|
| default constructor/dɪˈfɒlt kənˈstrʌktə/ | constructor por defecto |
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 · y 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 · Parámetros 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.
Two executions of new Student("Ada") refer to the same object because the arguments are equal.
Each successful new expression constructs a distinct object; equal arguments do not make the references equal.
Overloading constructors
- A class can have several constructors with different parameter lists — overloading.
Student(String n)and · y Student(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.
Constructors set the starting state · Los constructores establecen el estado inicial
Each new object is configured by the constructor's arguments.
A constructor has...
Same name as the class, no return type — not even void.
When does a constructor run?
new invokes the constructor to build the object.
If you write no constructor, Java provides a default no-argument constructor.
But writing any constructor removes that free default.
Once you define a constructor with parameters, new Student() still works automatically.
The free default is gone; write a no-arg constructor if you need one.
Writing two constructors with different parameter lists is an example of...
Same name, different parameters = overloading.
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 · y 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").
The supplied argument selects a matching constructor; declaring it does not also create a no-argument constructor.
A Counter has private int value = 7; and an explicit constructor that then assigns value = 9;. What is value immediately after construction?
The field initializer assigns 7 before the constructor body assigns 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 · Comparar
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.