Bifalgorithm post · Computer science one · Constructors
How can constructors call one another and copy objects?
How can constructors call one another and copy objects? (Computer science one · Constructors) — "In both of these examples, we repeated a lot of code. One shor…
Ideas in this flowchart
- Constructor reuse and copy constructors reduce repeated code.
- The examples contain a lot of repeated code.
- A shortcut is to make constructors call the most general constructor.
- The this keyword invokes another constructor as a method call.
- The no-argument Student constructor delegates with this(null, null, 0, 0.0).
- The two-argument Student constructor delegates with this(firstName, lastName, 0, 0.0).
- A copy constructor creates a copy of an instance passed to it.
- The copy constructor receives a Student instance as its parameter.
- It copies each member variable of s into this.