JavaBeans are classes that encapsulate many elements into a single object (the bean). It is a Java class that should follow the following conventions:
1. Must perform Serializable.
2. It should possess a public no-arg constructor.
3. All characteristics in java bean must be private with public getters and setter methods.
We don’t have a “struct” in C as it’s a keyword in C and has methods or functions in it. A struct is a collection of different data types and present in a class, Which is defined for JAVA class particularly.
JavaBeans are serializable, have a zero-argument constructor, and allow access to traits using getter and setter methods whereas a regular class in Java is nothing but a blueprint or a template for creating various objects which set its properties and JavaBean is so widely used because:
A Bean gets all the benefits of Java's "write-once, run anywhere" criterion.
The traits, events, and methods of a Bean that are presented to an application builder tool can be controlled.
The configuration settings of a Bean can be stored in resolute storage and restored at a later time.
The serializability of a class is approved by the class executing the java.io.Serializable interface. Classes that do not perform this interface will not have any of their state serialized or deserialized. The serialization interface has no arrangements or fields and assists only to recognize the interpretation of being serializable.
You can read here more about Serializable.
There’s no difference between JAVABeans and Regular JAVA class as A JavaBean is just a regular Java class that adapts to a number of conventions:
- There should be a public default constructor in it (i.e. a constructor that takes no arguments)
- it should be serializable
- it has getter and setter methods to get and set its properties