POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program. POJOs have gained the most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun Microsystems.
Properties of POJO
- Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { … } is not a POJO class.
- Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { … } is not a POJO class.
- Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { … } is not a POJO class.
POJOs basically define an entity. Like in your program, if you want an Employee class, then you can create a POJO as follows:
Java
public class Employee
{
String name;
public String id;
private double salary;
public Employee(String name, String id,
double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName()
{
return name;
}
public String getId()
{
return id;
}
public Double getSalary()
{
return salary;
}
}
|
Explaination of the above program:
The above example is a well-defined example of the POJO class. As you can see, there is no restriction on access-modifiers of fields. They can be private, default, protected, or public. It is also not necessary to include any constructor in it.
POJO is an object which encapsulates Business Logic. The following image shows a working example of the POJO class. Controllers interact with your business logic which in turn interact with POJO to access the database. In this example, a database entity is represented by POJO. This POJO has the same members as the database entity.

Java Beans
Beans are special type of Pojos. There are some restrictions on POJO to be a bean.
- All JavaBeans are POJOs but not all POJOs are JavaBeans.
- Serializable i.e. they should implement Serializable interface. Still, some POJOs who don’t implement a Serializable interface are called POJOs because Serializable is a marker interface and therefore not of many burdens.
- Fields should be private. This is to provide complete control on fields.
- Fields should have getters or setters or both.
- A no-arg constructor should be there in a bean.
- Fields are accessed only by constructor or getter setters.
Getters and Setters have some special names depending on field name. For example, if field name is someProperty then its getter preferably will be:
public "returnType" getSomeProperty()
{
return someProperty;
}
and setter will be
public void setSomePRoperty(someProperty)
{
this.someProperty=someProperty;
}
Visibility of getters and setters is generally public. Getters and setters provide the complete restriction on fields. e.g. consider below the property,
Integer age;
If you set visibility of age to the public, then any object can use this. Suppose you want that age can’t be 0. In that case, you can’t have control. Any object can set it 0. But by using the setter method, you have control. You can have a condition in your setter method. Similarly, for the getter method if you want that if your age is 0 then it should return null, you can achieve this by using the getter method.
Below is the implementation of the above topic:
Java
class Bean implements Serializable {
private Integer property;
public Bean()
{
}
public void setProperty(Integer property)
{
if (property == 0) {
return;
}
this.property = property;
}
public Integer getProperty()
{
if (property == 0) {
return null;
}
return property;
}
}
public class GFG {
public static void main(String[] args)
{
Bean bean = new Bean();
bean.setProperty(0);
System.out.println("After setting to 0: "
+ bean.getProperty());
bean.setProperty(5);
System.out.println("After setting to valid"
+ " value: "
+ bean.getProperty());
}
}
|
Output:
After setting to 0: null
After setting to valid value: 5
POJO vs Java Bean
| It doesn’t have special restrictions other than those forced by Java language. |
It is a special POJO which have some restrictions. |
| It doesn’t provide much control on members. |
It provides complete control on members. |
| It can implement Serializable interface. |
It should implement serializable interface. |
| Fields can be accessed by their names. |
Fields are accessed only by getters and setters. |
| Fields can have any visibility. |
Fields have only private visibility. |
| There may/may-not be a no-arg constructor. |
It must have a no-arg constructor. |
| It is used when you don’t want to give restriction on your members and give user complete access of your entity |
It is used when you want to provide user your entity but only some part of your entity. |
Conclusion
POJO classes and Beans both are used to define java objects to increase their readability and reusability. POJOs don’t have other restrictions while beans are special POJOs with some restrictions.
Similar Reads
Introduction of BlueJ
BlueJ is a windows based platform for Java Development Kit (JDK). It is a free Java environment started in 1999 by Michael Kolling and John Rosenberg at Monash University, Australia, as a successor to Blue. It requires to install JDK version 1.3 or more before installing BlueJ. It can be freely down
3 min read
Apache POI | Getting Started
POI stands For "Poor Obfuscation Implementation". Apache POI is an API provided by Apache foundation which is a collection of different java libraries. These libraries gives the facility to read, write and manipulate different Microsoft files such as excel sheet, power-point, and word files. It's fi
4 min read
Implementing Message Queuing Technique in Netbeans Through Enterprise Java Beans
A Message Queues, allow one or more processes to write a message to be read by other processes. A message queue is implemented as a linked list of messages and is stored within the Kernel. Each message queue is identified by a message queue identifier. The Kernel keeps track of all the message queue
5 min read
How is CompileTime classpath different from RunTime classpath in Java?
What is a Classpath? Classpath refers to the Path where all the classes/jars are available. What is Compile-time classpath? This classpath has all the classes/jars required to compile the application without any syntax error. Now we might think that our project compiled successfully so it will execu
2 min read
Difference between Javabeans and Enterprise Javabeans
Notejava.beans: This package has been deprecated in Java 9 and later versions, in favor of using annotations and other modern ways of creating beans. Using it in production code is not recomended.JavaBeans: JavaBeans are recyclable software components for Java which by using a builder tool can be ma
2 min read
Difference between EJB and Spring
EJB and Spring both are used to develop enterprise applications. But there are few differences exists between them. So, in this article we have tried to cover all these differences. 1. Enterprise Java Beans (EJB) : EJB stand for Enterprise Java Beans. It is a server side software component that summ
3 min read
JavaBean class in Java
JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a Java class that should follow the following conventions: Must implement Serializable.It should have a public no-arg constructor.All properties in java bean must be private with public getters and setter meth
3 min read
Introduction to Spring Framework
The Spring Framework is a powerful, lightweight, and widely used Java framework for building enterprise applications. It provides a comprehensive programming and configuration model for Java-based applications, making development faster, scalable, and maintainable. Before Enterprise Java Beans (EJB)
9 min read
Spring - FactoryBean
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made
5 min read
Difference between BlueJ and JDK 1.3
Java has been one of the most popular programming languages for many years. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In this article, the difference between BlueJ, a Java IDE and JDK is dis
3 min read