Java allows to summarize this.classVar = parameter; this.classVar2 = parameter2;
expressions to this(parameter, parameter2)
. At least used in a constructor.
But this code doesn't work when I change from the former way (commented in the code) to the latter way in a setter:
class Client {
String nombre, apellidos, residencia;
double comision;
void setClient(String nombre, String apellidos, String residencia, double comision){
this(nombre, apellidos, residencia, comision);
//this.nombre = nombre;
//this.apellidos = apellidos;
//this.residencia = residencia;
//this.comision = comision;
}
}
Error says:
"call to this must be first statement in the constructor.
Constructor in class Client cannot be applied to given types.
required: no arguments
<p>found: String, String, String, double
<p>reason: actual and formal argument list differ in length" (I haven't created one, just left the default).
So, is this way of using 'this
' only valid for constructors, and therefore not suitable for setters? Does it require to explicitly code the constructor (if so, why?)?