Builder of builder?

I would like to understand why this class has two constructors and why one of them is all within the same this and not separated as in the other. Does that change anything?

Normal Constructor:

public Conta(Correntista correntista, String senha, double saldo) {
    Random gerador = new Random();
    this.numeroDaConta = gerador.nextInt(9000)+1000;
    this.correntista = correntista;
    this.senha = senha;
    this.saldo = saldo;
}

Constructor of the constructor:

public Conta(Correntista correntista, String senha) {
    this(correntista, senha, 0);
}
Author: Guilherme Bigois, 2017-06-24

3 answers

Aconstructor is not sodifferent from a common method . Just as methods can be overloaded , constructors can too. So you can have methods with the same name and different signatures . This makes it another method. This is useful because everyone can have a convenience.

In your case the first would be a complete constructor that does everything it needs. The second only delegates to the first how to do the construction. So you do not need to pass the balance if it is a default value, call without it and the secondary constructor takes care of passing the balance to you.

This is not necessary in languages that have arguments with default value, which is not the case with Java, at least yet.

There is a special syntax to show that you are calling a constructor and not just any method. Is to call the method this. This way the compiler can treat it differently. Then the constructor secondary does not construct the object, primary will. But it is he who will deliver the result, if it is he who was invoked in the instantiation of the object.

This syntax is necessary because there may be non-constructor methods with the same class name.

Note that the this in the other constructor is used to disambiguate the local variable or parameter of the object attribute. So it's not all within the same this, it's the same syntax for engines different.

this(correntista, senha, 0);

In this case is the same as

Conta(correntista, senha, 0);

Which cannot be called inside a constructor correctly always (there may be ambiguity).

Would anyone think they might have done the same code in the two constructors. But this violates the DRY and can create maintenance difficulty. This is not to save line of code, it has important semantic purpose. There's no time gain in that.

Understand that this is even a kind of polymorphism, but we usually call it overload, because the understanding of polymorphism usually refers to something else. This terminology is somewhat ambiguous, overload is not.

I will not comment on the other problems of this method that is not focus of the question and for an exercise is ok.

 7
Author: Maniero, 2020-05-26 17:48:53

Hello, using various constructor methods allows you to build objects in many ways. That way, once you have declared this constructor:

public Conta(Correntista correntista, String senha, double saldo) {
  Random gerador = new Random();
  this.numeroDaConta = gerador.nextInt(9000)+1000;
  this.correntista = correntista;
  this.senha = senha;
  this.saldo = saldo;
}

And you feel the need to create objects of Type account without passing a balance, you can declare a constructor that only receives as argument a current object and the password:

public Conta(Correntista correntista, String senha) {
    this(correntista, senha, 0);
}

That way, once you have a more "complete" constructor method that already does all the necessary assignments, just call this more "complete" constructor within the new constructor, passing as argument the attribute that the new constructor does not receive, in this case the balance.

When you call in the second constructor method:

this(correntista, senha, 0);

Indirectly you are calling the first constructor method that receives three arguments. Since the first constructor is responsible for assigning parameters to target attributes, the second constructor method only uses a this, and passes the entire responsibility to the first constructor method.

 2
Author: Thiago Inocêncio, 2017-06-24 17:24:09

Hello, What was done in the code above is called polymorphism. This is a method overload.

Well let's say in the code above you want to create an account, and that account can be created either without initial balance or with initial balance. When creating an account with initial balance:

Conta novaConta = new Conta(new Correntista(),"teste",30.30);

Calling new Conta will invoke the Class Account and within it will be called a constructor. At this point, the method that best suits the call parameters will be executed. In this case the first code you posted.

In case of creating a new account without the balance, the programmer could have done the method like this:

public Conta(Correntista correntista, String senha) {
    Random gerador = new Random();
    this.numeroDaConta = gerador.nextInt(9000)+1000;
    this.correntista = correntista;
    this.senha = senha;
    this.saldo = 0;
}

But to save line of code and time what it does is, it calls the other constructor with argument that has balance through the this and puts a starting balance 0. Thus saving code and time.

Does not change anything, it was only made to save time and suit the call, you can read a little more about polymorphism here

 1
Author: Um Programador, 2017-06-24 16:16:56