Java Beans

Java Beans are reusable software components according to the specification. A Java Bean can encapsulate many objects into a single object, and we can use them through “get” and “set” methods in various places in our application. Besides providing eas…


This content originally appeared on DEV Community and was authored by Diego Brener

Java Beans

Java Beans are reusable software components according to the specification. A Java Bean can encapsulate many objects into a single object, and we can use them through "get" and "set" methods in various places in our application. Besides providing easy maintenance, it also allows our objects to be eligible for other tools, such as persistence tools.

To be defined as Java Beans, the class must follow some conventions, such as:

  • Implement the java.io.Serializable interface (to allow the object's state to be persisted and retrieved).
  • Have a no-argument constructor (no-arg constructor).
  • Have private properties accessible by "get" and "set" methods.

Example of a bean:

// Implements Serializable
public class Pessoa implements java.io.Serializable {

    // Has private properties
    private String nome;

    // Get methods
    public String getNome() {
        return nome;
    }

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


    /* No-argument constructor */
    public Pessoa() {
        this.nome="";
    }
}


This content originally appeared on DEV Community and was authored by Diego Brener


Print Share Comment Cite Upload Translate Updates
APA

Diego Brener | Sciencx (2024-07-21T00:12:39+00:00) Java Beans. Retrieved from https://www.scien.cx/2024/07/21/java-beans/

MLA
" » Java Beans." Diego Brener | Sciencx - Sunday July 21, 2024, https://www.scien.cx/2024/07/21/java-beans/
HARVARD
Diego Brener | Sciencx Sunday July 21, 2024 » Java Beans., viewed ,<https://www.scien.cx/2024/07/21/java-beans/>
VANCOUVER
Diego Brener | Sciencx - » Java Beans. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/21/java-beans/
CHICAGO
" » Java Beans." Diego Brener | Sciencx - Accessed . https://www.scien.cx/2024/07/21/java-beans/
IEEE
" » Java Beans." Diego Brener | Sciencx [Online]. Available: https://www.scien.cx/2024/07/21/java-beans/. [Accessed: ]
rf:citation
» Java Beans | Diego Brener | Sciencx | https://www.scien.cx/2024/07/21/java-beans/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.