What is a Java Bean and what is it for?

I'm starting to read about layered java development, and I've read something about bean, an encapsulated class.

But I couldn't find any content that actually explained the purpose and an example.

So, what is a Bean and what is it for?

Author: gmsantos, 2014-06-25

3 answers

Good didn't get too specific about what exactly you're talking about, in case the answer isn't exactly what you need, please specify.

What is a Bean and what is it for ?

In short:

  • a class that contains all private attributes
  • own getters and setters for your {[13] Attributes]}
  • used to encapsulate and abstract an entity
  • implement java.io.Serializable

JavaBean is a class extremely simple, the example below reflects a "user" in some system. That is, it is just a class that has only attributes and their respective getters and setters... in general it does not have any additional logic.

Not to be confused with Bean, which is a component of the Java EE{[4] enterprise Java Bean (EJB) specification]}

Example:

    public class Usuario {

    private String nome;
    private String sobrenome;
    private int idade;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }
}

Note that there is a convention in the nomenclature of the getters and setters. Here you can find more details about the standard nomenclature.

* * Edit * * * * *

Ok, this is a Bean, but what is it for, what is its function ?

A JavaBean represents an entity (or unit or model) of the system (UML here!). It encapsulates the information needed to be carried along the layers or modules. In other cases it also maps these entities to the database, see JPA, or also in integrations between applications (Web services JAX-RS for example).

Summarizing all necessary data in one place.

To better understand, it is necessary to think more like system modeling rather than programming itself:

//Ok, eu sei que na instância de 'user' eu tenho todas as informações
//necessárias e encapsuladas ao meu modelo(entidade) "USUÁRIO".
Usuario user = new Usuario();

If it's not exactly what you're looking for, just return.

 22
Author: Josh, 2016-09-02 16:59:58

I also find the term bean somewhat obscure. In Java the pun justifies itself, since beans represent the elements from which the product (coffee, in the case) is made.

Eric Evans proposes a simpler language for the software development Universe. For him, the ubiquitous language (ubiquitous, to which everyone speaks) is the basis for everyone to understand the system. So, in order to facilitate understanding, call a group of beans and each bean is an entity. As already said, beans are useful to represent the core of the business, that is, the elements on which it is vital to manage information.

 2
Author: Vítor Neil Avelino, 2014-09-03 14:16:32

Simplifying the way of explanation, the JavaBean standard, is nothing more than classes that model objects, where, obligatorily, the attributes are declared private, that there is a default public constructor, and has to have the accessor methods(getters and setters) public for each attribute, and only that. As the example of the Usuario class.java;

public class Usuario {

    private Long idUsuario;
    private String nome;
    private String sobrenome;
    private Integer idade;

    public Usuario(){
    }

    public Long getIdUsuario(){
        return idUsuario;
    }

    public void setIdUsuario(Long idUsuario){
        this.idUsuario = idUsuario;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }
}
 1
Author: Tiago Ferezin, 2015-06-16 18:32:58