Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)

I understood I think, that a "Bean" is a Java class with properties and getters/setters. As much as I understand, it is the equivalent of a C struct. Is that true?

Also, is there a real syntactic difference between a bean and a regular class? Is there any special definition or an interface?

Basically, why is there a term for this?

Edit: If you can be so kind and add information regarding the Serializable interface, and what it means.

To your answer, I'd be very grateful.

1 Answer

0 votes
by (46k points)

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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 20, 2019 in Java by Anvi (10.2k points)

Browse Categories

...