JavaBean
The JavaBeans are a component model created by Sun Microsystems for building Java applications.
They are used to encapsulate several objects in a single object (the pod or Bean in English), to make use of a single object instead of several simpler ones.
The Sun Microsystems JavaBeans specification defines them as "reusable software components that can be manipulated visually in a build tool".
Despite many similarities, JavaBeans should not be confused with Enterprise JavaBeans (EJB), a server-side component technology that is part of Java EE.
JavaBean Conventions
To function as a JavaBean class, a class must obey certain conventions about method naming, construction, and behavior.
These conventions allow for tools that can use, reuse, replace, and plug in JavaBeans.
The required conventions are:
- You must have a builder without arguments.
- His class attributes must be private.
- Its properties should be accessible through get and set methods that follow a standard nomenclature convention.
- It must be serializable.
Structure
Within a JavaBean we can distinguish three parts:
- Properties: The attributes it contains.
- Methods: Methods are established
get
andset
to access and modify the attributes. - Events: Allow to communicate with other JavaBeans.
Example
// Implementation of the Serializable interface of the java.io package public class PersonBean implements java.io.Serializable { // Each instance variable has a private access modifier. private String Name; private int Age; // Empty builder and without arguments. public PersonBean() { ! // Optional builder of a JavaBean. public PersonBean(String Name, int Age) { this.Name = Name; this.Age = Age; ! // Builder per copy (optional) public PersonBean(PersonBean personBean) { this.Name = personBean.getName(); this.Age = personBean.getEdad(); ! // For each property add a get and set method. public String getName() { return Name; ! public void set(String Name) { this.Name = Name; ! public int getEdad() { return Age; ! public void setEdad(int Age) { this.Age = Age; !!
Contenido relacionado
Hogwarts School of Witchcraft and Wizardry
Monforte de la sierra
Back to the Future
Klaus kinski
10 (film)