What is a Java Bean Class

It is a Java class that is developed with some standards. The standards are:

  • Must be public and should not be a final or abstract class.
  • Can implement Serializable(I) if needed.
  • Must have a direct or indirect 0-param constructor.
  • Member variables are called bean properties and they must be taken as private and non-static.
  • Every bean property should have setter and getter methods(public).

Example:

  //Person class represents Java Bean class 
 class Person {
   //private id property
   private Integer id;
   //private id property
   private String name;
   //private mobileNo property
   private Integer mobileNo;

   //0-param constructor
   public PersonBean() {}

   //getter method of the id property
   public int getId() {
     return id;
   }
   //setter method of the id property
   public void setId(int id) {
     this.id = id;
   }
   //getter method of the name property
   public String getName() {
     return name;
   }
   //setter method of the name property
   public void setName(String name) {
     this.name = name;
   }
   //getter method of the mobileNo property
   public Integer getMobileNo() {
     return mobileNo;
   }
   //setter method of the mobileNo property
   public void setMobileNo(Integer mobileNo) {
     this.mobileNo = mobileNo;
   }
 }

  • In the above example we created a Bean class with the name “Person”. Properties of this class id, name and mobileNo and modifiers of these properties are private. This Java class contains a 0-param constructor. This class also contains a setter and getter method for each property for sending and retrieving data from one layer to another layer. This is an example of the Java Bean class that uses every property of the Java Bean in this Person class to make a Java Bean class.
  • In real time we will use Java Bean as a Model class. When sending data from one layer to another (like View tier to Service tier etc.).
  • Java Bean class helps to use readability and reusability features in the code.
  • Java Bean class is the POJO class which follows some standards.
  • Java Bean class follows security that’s why all properties must be private.
  • The Java Bean class is very helpful for developers because they develop real-time projects. Suppose in these projects Model classes have a lot of properties to manage with the help of their constructors to initialize when instantiating them. It is very difficult. The Java Bean class solve this problem with its getter and setter methods for sending and retrieving data from one class to another because all setter and getter are public modifiers used.

POJO class  vs Java Bean class

POJO class

  • The Pojo class doesn’t have any restrictions to follow some standards.
  • The Pojo class doesn’t have much control over the members.
  • The Pojo class members are visible for other classes.
  • The Pojo class has properties that can be accessible.
  • The Pojo class may contain a parameterised constructor.

Java Bean Class

  • The Java Bean class has certain restrictions to follow some standards.
  • The Java Bean class has much control over the members.
  • The Java Bean class members are not visible for other classes.
  • The Java Bean class has properties that can be accessible by their getter methods.
  • The Java Bean class contains the 0-param constructor.

We can use Lombok annotation in the Java Bean class to generate automatic default constructor, setter and getter methods.

Conclusion:

This topic is explained What is a Java Bean class? What are the rules to make a Java Bean class?

Leave a Comment