JavaBeans
JavaBeans are class definitions following a set of particular conventions. They are not tied to any Java framework so any Java program can use them. All JavaBeans can be considered POJOs (plain old Java objects). Many Java libraries and frameworks rely on the JavaBean conventions.
Syntax
The following syntax conventions are used in JavaBeans:
- All properties are private and the class exposes them through getter and setter methods.
- All getters and setters are named “getX” or “setX”. In the case of a boolean property, “isX” can be used for a getter.
- A default constructor that requires no arguments must be present.
- It implements the
Serializable
interface, allowing the program to store its state.
Note: In implementing a Serializable class, it is strongly recommended to declare an explicit
serialVersionUID
value. While the explicit declaration is optional, the generated values might vary from compiler to compiler, leading to an unexpectedInvalidClassException
during deserialization.serialVersionUID
should be astatic
final
long
, and it is recommended to beprivate
.
Example
The following is an example of a JavaBean:
// Student.javaimport java.io.Serializable;public class Student implements Serializable {private static final long serialVersionUID = 12345678Lprivate String firstName;private String lastName;private String major;public Student() {}public Student(String firstName, String lastName, String major) {this.firstName = firstName;this.lastName = lastName;this.major = major;}public String getFirstName() {return this.firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return this.lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getMajor() {return this.major;}public void setMajor(String major) {this.major = major;}}
Potential Disadvantages
The JavaBeans standard does enforce some characteristics that may be disadvantageous depending on the implementation:
- The default constructor allows the object to be instantiated without initializing its properties. Depending on the object, this may be considered an invalid state.
- The creation of getters and setters may be unnecessary for an implementation and may result in a lot of unnecessary coding.
- The JavaBean is mutable due to its setter methods which might lead to concurrency or memory consistency issues.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours