Question about initialized attributes in the constructor in Java

Asked

Viewed 557 times

2

package Livraria3;

public class Livro {

    private String nome;
    private String descricao;
    private double valor;
    private String isbn;
    Autor autor;

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

    }

    public Livro() {

    }

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getDescricao() {
        return descricao;
    }
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
    public double getValor() {
        return valor;
    }
    public void setValor(double valor) {
        this.valor = valor;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public Autor getAutor() {
        return autor;
    }
    public void setAutor(Autor autor) {
        this.autor = autor;
    }

    public void mostrarDetalhes() {

           System.out.println("Mostrando detalhes do livro:");
           System.out.println("Nome: " + nome);
           System.out.println("Descricao: " + descricao);
           System.out.println("Valor: " + valor);
           System.out.println("Isbn: " + isbn);
           System.out.println("--");

           autor.mostrarDetalhes();

        }


}
 package Livraria3;

    public class Autor {

        private String nome;
        private String email;
        private String cpf;

        public String getNome() {
            return nome;
        }
        public void setNome(String nome) {
            this.nome = nome;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getCpf() {
            return cpf;
        }
        public void setCpf(String cpf) {
            this.cpf = cpf;
        }

        public void mostrarDetalhes() {

              System.out.println("Mostrando detalhes do Autor:");
              System.out.println("Nome: " + nome);
              System.out.println("Email: " + email);
              System.out.println("CPF: " + cpf);

              }
    }
package Livraria3;

public class CadastroDeLivros {
    public static void main(String[] args) {

        Autor autor = new Autor();
        autor.setNome("Leonardo Silva");
        autor.setEmail("[email protected]");
        autor.setCpf("231.980.523.66");

        Livro livro = new Livro(autor);
        livro.setNome("Programacao em Java");
        livro.setDescricao("Livro de Java");
        livro.setValor(79.90);


        //livro.setAutor(autor);
        livro.mostrarDetalhes();


        Autor outroAutor = new Autor();
        outroAutor.setNome("Tiago Vieira");
        outroAutor.setEmail("[email protected]");
        outroAutor.setCpf("331-453-234-75");

        Livro outroLivro = new Livro(outroAutor);
        outroLivro.setNome("Programacao em Cpp");
        outroLivro.setDescricao("Livro de cpp");
        outroLivro.setValor(89.90);
        outroLivro.setIsbn("459-94-9103-15-34");

        //outroLivro.setAutor(outroAutor);
        outroLivro.mostrarDetalhes();

}
}

Is it any different than when I initialize an object attribute in the class constructor, such as:

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

Or use here?

Livro livro = new Livro(autor);
        livro.setNome("Programacao em Java");
        livro.setDescricao("Livro de Java");
        livro.setValor(79.90);
      //livro.isb = "567-67-667-66-77;

2 answers

2


Is there any difference when I start an attribute of an object in class builder?

Yes, by exposing only one constructor, with the attributes of the class you want to create, you can force those who call your constructor to pass all the parameters necessary to create the object. Whereas if you use Setters, Someone might not call the setters necessary for the object to be in a valid state. That would be one of the main differences. There is even an exception in Java for when your object is not in a valid state:

http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalStateException.html

Now, whether it’s right or wrong, or better, it’s hard to say because it depends a lot on the design of your program, or your API.

  • Sorry, but I can’t read the API.

  • Hi @Nome, vc refers to the page in English, or the term API?

  • Very technical, not because it’s in English.

  • Over time you get used to it. Just keep practicing. =)

1

Both will work, but there are important structural differences.

When initializing everything in the constructor, it is easier to ensure encapsulation as you are not required to publish setters.

In addition, you guarantee that the object will be created in a state that makes sense. Otherwise, you will probably create an object in an incoherent state and be forced to fix it afterwards, which is bad.

  • The.0 easiest maintenance.

  • @Exact Name, maintenance gets easier. Your object gets easier to use in the right way and harder to use in the wrong way.

Browser other questions tagged

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