Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a JavaBean exactly?

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 JavaBean and a regular class?
Is there any special definition or an Interface?

Basically, why is there a term for this?

Also what does the Serializable interface mean?

like image 942
Amir Rachum Avatar asked Jul 21 '10 00:07

Amir Rachum


People also ask

What is JavaBean?

A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. It provides a default, no-argument constructor. It should be serializable and that which can implement the Serializable interface.

Why do we use JavaBean?

Why use JavaBean? According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object so that we can access this object from multiple places. Moreover, it provides easy maintenance.

Where is JavaBean used?

Beans in web applications Also transferring that data around the application is easy too since JavaBeans help you to decouple parts of your application completely. Think JavaBeans as a letter and various subsystems of the application as departments within a very large corporation: Dept. A mails a bunch of data to Dept.


1 Answers

A JavaBean is just a standard

  1. All properties are private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.

That's it. It's just a convention. Lots of libraries depend on it though.

With respect to Serializable, from the API documentation:

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

In other words, serializable objects can be written to streams, and hence files, object databases, anything really.

Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.

There is a term for it, because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the library requires your objects be proper JavaBeans).

like image 145
hvgotcodes Avatar answered Oct 13 '22 09:10

hvgotcodes