The Student class above will allow Student information to be constructed in various ways, but is repetitive. How could you use the this reference to reduce the amount of repetitious code and make it less error-prone?

What will be an ideal response?

You use the this reference from one constructor version to call another version. You save coding and reduce the chance for errors by writing each constructor to call one master constructor. The Student class could be rewritten as follows:


For example, if code is added later to ensure that all student ID numbers are three digits, or that no grade point average is greater than 4.0, the new code will be written only in the two-parameter version of the constructor, and all the other versions will use it.


Although you can use the this reference with field names in any method within a class, you cannot call this() from other methods in a class; you can only call it from constructors. Additionally, if you call this() from a constructor, it must be the first statement within the constructor.


Computer Science & Information Technology

You might also like to view...

Given the infix expression (( a + b) * c Convert it to a prefix expression.

What will be an ideal response?

Computer Science & Information Technology

To delete records in a table, use the keywords ____.

A. DELETE, RECORD B. DELETE, WHERE C. REMOVE, WHERE D. REMOVE, RECORD

Computer Science & Information Technology