0

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?)?

Hearen
  • 7,420
  • 4
  • 53
  • 63
Martin
  • 414
  • 7
  • 21
  • 1
    You are calling the constructor with the argument list: `String, String, String, double`. There is no such constructor. Also you can't call a constructor like that from within a method. – Ben Jun 11 '18 at 10:39
  • Yes. `this` is only for calling a constructor from a different one. You do need to code it otherwise you can't call it. – daniu Jun 11 '18 at 10:41

3 Answers3

3

Java allows to summarize this.classVar = parameter; this.classVar2 = parameter2; expressions to this(parameter, parameter2).

No, it doesn't. You still have to code the this.classVar = parameter; this.classVar2 = parameter2; somewhere. All this(parameter, parameter2) does is call a constructor (which would have to have the this.classVar = parameter; this.classVar2 = parameter2; code in it, if those parameters were going to be written to those fields).

You can't call the constructor from a setter. You can only call a constructor from within a constructor. It's used to consolidate logic in a single constructor even when you have more than one with varying parameters, for example:

public MyContainer(int size) {
    this.size = size;
}
public MyContainer() {
    this(16);
}

There, the zero-parameters version of the MyContainer constructor calls the single-parameter version, passing it 16 for the size parameter.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • just adding a bit: the 'Explicit Cnstructor Invocation' like `this(...)` must be the the first statement of an(other) constructor – user85421 Jun 11 '18 at 11:34
2

this(nombre, apellidos, residencia, comision) doesn't "summarize" anything.

It's just a way to call another constructor in the class from a constructor.

There is no way to "summarize" anything

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
1
this(/* zero or more args */);

This is a constructor call. You can use it from one constructor to refer to another (for lack of a better name, 'constructor chaining').

You cannot do the same thing from a normal method. If you want to create an object from within a normal method, you use the same syntax as you'd use an an external user of the class:

new MyClass(/* args */);

From your code, it doesn't look like this is the approach you'd want to take.

Michael
  • 41,989
  • 11
  • 82
  • 128