java - What is a JavaBean exactly?

ID : 417

viewed : 54

Tags : javadependency-injectioninversion-of-controljavabeansserializablejava

Top 5 Answer for java - What is a JavaBean exactly?

vote vote

90

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).

vote vote

85

There's a term for it to make it sound special. The reality is nowhere near so mysterious.

Basically, a "Bean":

  • is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
  • has "properties" whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the "Foo" property), and
  • has a public zero-argument constructor (so it can be created at will and configured by setting its properties).

As for Serializable: That is nothing but a "marker interface" (an interface that doesn't declare any functions) that tells Java that the implementing class consents to (and implies that it is capable of) "serialization" -- a process that converts an instance into a stream of bytes. Those bytes can be stored in files, sent over a network connection, etc., and have enough information to allow a JVM (at least, one that knows about the object's type) to reconstruct the object later -- possibly in a different instance of the application, or even on a whole other machine!

Of course, in order to do that, the class has to abide by certain limitations. Chief among them is that all instance fields must be either primitive types (int, bool, etc.), instances of some class that is also serializable, or marked as transient so that Java won't try to include them. (This of course means that transient fields will not survive the trip over a stream. A class that has transient fields should be prepared to reinitialize them if necessary.)

A class that can not abide by those limitations should not implement Serializable (and, IIRC, the Java compiler won't even let it do so.)

vote vote

76

JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to

  1. implement the java.io.Serializable interface - to save the state of an object
  2. use a public empty argument constructor - to instantiate the object
  3. provide public getter/setter methods - to get and set the values of private variables (properties).
vote vote

61

Properties of JavaBeans

A JavaBean is a Java object that satisfies certain programming conventions:

  1. The JavaBean class must implement either Serializable or Externalizable

  2. The JavaBean class must have a no-arg constructor

  3. All JavaBean properties must have public setter and getter methods

  4. All JavaBean instance variables should be private

Example of JavaBeans

@Entity public class Employee implements Serializable{     @Id    private int id;    private String name;       private int salary;       public Employee() {}     public Employee(String name, int salary) {       this.name = name;       this.salary = salary;    }    public int getId() {       return id;    }    public void setId( int id ) {       this.id = id;    }    public String getName() {       return name;    }    public void setName( String name ) {       this.name = name;    }    public int getSalary() {       return salary;    }    public void setSalary( int salary ) {       this.salary = salary;    } } 
vote vote

51

Explanation with an example.

1. import java.io.Serializable

As for the Serialization, see the documentation.

2. private fields

Fields should be private for prevent outer classes to easily modify those fields. Instead of directly accesing to those fields, usuagly getter/setter methods are used.

3. Constructor

A public constructor without any argument.

4. getter/setter

Getter and setter methods for accessing and modifying private fields.

/** 1. import java.io.Serializable */ public class User implements java.io.Serializable {     /** 2. private fields */     private int id;     private String name;      /** 3. Constructor */     public User() {     }     public User(int id, String name) {         this.id = id;         this.name = name;     }      /** 4. getter/setter */     // getter     public int getId() {         return id;     }     public String getName() {         return name;     }     // setter     public void setId(int id) {         this.id = id;     }     public void setName(String name) {         this.name = name;     } } 

Top 3 video Explaining java - What is a JavaBean exactly?

Related QUESTION?