Java Bean VS Java POJO

As in development we have used POJO and Beans many time without considering the actual difference between them as for sake sometimes we don’t consider both of them different anymore. So it’s worth to discuss about the difference. Even Java doc fails to distinguish between them in deep. But still it has some basic differences which we will discuss today.

Java Bean : Must follow specific conventions

POJO(Plain Old Java Object) : No specific convention or restrictions

Let’s checkout the conventions that Java Bean MUST follow,

  1. No arg Constructor (Default Constructor Declared)
  2. Getters and Setters for all the properties (get and set methods)
  3. Serializable Or Externalizable (implement Serializable or Externalizable interface)

Consider the following example which is purely a Java Bean ,


import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable {
     /**
     *
     */
     private static final long serialVersionUID = 1L;

     private int rollNo;

     private String name;

     private String surName;

     private Date dob;

     public Student() {

     }

     public int getRollNo() {
         return rollNo;
     }

     public void setRollNo(int rollNo) {
         this.rollNo = rollNo;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public String getSurName() {
         return surName;
     }

     public void setSurName(String surName){
        this.surName = surName;
     }

     public Date getDob() {
        return dob;
     }

     public void setDob(Date dob) {
         this.dob = dob;
     }
}

While considering POJO it does not have any convention to be followed but it is just declared with some sort of properties and getter/setters (not mendatory)  and which does not need to implement specific interface of Java API.

Java Bean serve special role and mainly created as a reusable entity while POJO doesn’t aim that.

So in short Java bean is mainly created for global purpose and mainly aimed to be reused at many fragments of the program. While POJO is not created for that aim at least and it has no specific structure to be followed, it is just simple Java Object class as stated below.


import java.util.Date;

public class Student {

     private int rollNo;

     private String name;

     private String surName;

     private Date dob;

     public int getRollNo() {
         return rollNo;
     }

     public void setRollNo(int rollNo) {
         this.rollNo = rollNo;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public String getSurName() {
         return surName;
     }

     public void setSurName(String surName) {
        this.surName = surName;
     }

     public Date getDob() {
        return dob;
     }

     public void setDob(Date dob) {
         this.dob = dob;
     }
}

Java Bean bound by some conventions while Java POJO is not.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s