What does this() do alone in the builder?

Asked

Viewed 600 times

13

In the code:

public Livro(Autor autor) { 
        this();
        this.autor = autor;
    }
    public Livro() { 
        this.isbn = "000-00-00000-00-0";
    }

3 answers

13

this is a reference to the current object - the object whose method or constructor is being called.

When used inside a constructor serves to call another constructor in the same class.

Your Class Book declares two builders:

public Livro(Autor autor)

and

public Livro()

In the builder public Livro() the field is initialized isbn which must (I suppose) be initialized whatever the constructor used, hence the need for it to be called by the constructor public Livro(Autor autor).

Another common reason to use this is when a method or constructor parameter has a "name" equal to a class field. Look at the builder public Livro(Autor autor), whose parameter is autor, it was necessary to use this.autor = autor; to differentiate the field parameter.

I believe that the code you posted is just one example, but I leave here a "more correct" way of declaring manufacturers:

public class Livro{

    private String isbn;
    private Autor autor; 

    public Livro(Autor autor, String isbn) { 
        this.autor = autor;
        this.isbn = isbn;
    }

    //Talvez passar ""(empty) na vez de "000-00-00000-00-0"
    public Livro(Autor autor) { 
        this(autor, "000-00-00000-00-0");
    }

    //Talvez não se devesse criar um livro sem autor.
    //Ou talvez sim, se o autor for desconhecido.
    public Livro() { 
        //Em vez de null talvez fosse preferível criar um Autor "default"(desconhecido)
        this(null);
    }
}
  • I didn’t understand the attribute private Autor autor; , why does the first method take this as a parameter? Would it be a class? And it has to pass classes as parameters?

  • 1

    @Jarwin Author is a class, the attribute is needed to store the instance(object) of the Author class that is passed(o) in the constructor.

10


It invokes the constructor that does not receive parameters, for example in the expression Livro l = new Livro(autor); what will occur is the following:

  1. new Livro(autor) will invoke the book builder that receives an "author" as a parameter, in this case public Livro(Autor autor) {
  2. Internally the builder public Livro(Autor autor) { will call this() which will invoke the Book constructor that does not receive any parameter, in this case public Livro() {

You could rewrite the code in the following equivalent way:

public Livro(Autor autor) { 
    this.isbn = "000-00-00000-00-0"; // ao invés de invocar o construtor Livro() estamos ralizando o que o tal construtor faz.
    this.autor = autor;
}
public Livro() { 
    this.isbn = "000-00-00000-00-0";
}
  • If isbn were declared in the first constructor what the author receives as parameter, when creating a new book object and without author it does not receive isbn, but as null.

1

First we have the principles:

  • In a short way, every class has by default an empty constructor.

  • By definition, this refers to the class itself.

Following the definitions, this() refers to the default constructor of a Java class =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.