
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: June 11, 2024
In this short tutorial, we’ll investigate the definition of “Plain Old Java Object” or POJO for short.
We’ll look at how a POJO compares to a JavaBean, and how turning our POJOs into JavaBeans can be helpful.
When we talk about a POJO, what we’re describing is a straightforward type with no references to any particular frameworks. A POJO has no naming convention for our properties and methods.
Let’s create a basic employee POJO. It’ll have three properties; first name, last name, and start date:
public class EmployeePojo {
public String firstName;
public String lastName;
private LocalDate startDate;
public EmployeePojo(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String name() {
return this.firstName + " " + this.lastName;
}
public LocalDate getStart() {
return this.startDate;
}
}
This class can be used by any Java program as it’s not tied to any framework.
But, we aren’t following any real convention for constructing, accessing, or modifying the class’s state.
This lack of convention causes two problems:
First, it increases the learning curve for coders trying to understand how to use it.
Second, it may limit a framework’s ability to favor convention over configuration, understand how to use the class, and augment its functionality.
To explore this second point, let’s work with EmployeePojo using reflection. Thus, we’ll start to find some of its limitations.
Let’s add the commons-beanutils dependency to our project:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
And now, let’s inspect the properties of our POJO:
List<String> propertyNames =
PropertyUtils.getPropertyDescriptors(EmployeePojo.class).stream()
.map(PropertyDescriptor::getDisplayName)
.collect(Collectors.toList());
If we were to print out propertyNames to the console, we’d only see:
[start]
Here, we see that we only get start as a property of the class. PropertyUtils failed to find the other two.
We’d see the same kind of outcome were we to use other libraries like Jackson to process EmployeePojo.
Ideally, we’d see all our properties: firstName, lastName, and startDate. And the good news is that many Java libraries support by default something called the JavaBean naming convention.
A JavaBean is still a POJO but introduces a strict set of rules around how we implement it:
So, let’s try converting EmployeePojo into a JavaBean:
public class EmployeeBean implements Serializable {
private static final long serialVersionUID = -3760445487636086034L;
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeeBean() {
}
public EmployeeBean(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// additional getters/setters
}
When we inspect our bean with reflection, now we get the full list of the properties:
[firstName, lastName, startDate]
So, we’ve shown a way in which JavaBeans are helpful. Keep in mind that every design choice comes with tradeoffs.
When we use JavaBeans we should also be mindful of some potential disadvantages:
Given these tradeoffs, frameworks have also adapted to other bean conventions over the years.
In this tutorial, we compared POJOs with JavaBeans.
First, we learned a POJO is a Java object that is bound to no specific framework, and that a JavaBean is a special type of POJO with a strict set of conventions.
Then, we saw how some frameworks and libraries harness the JavaBean naming convention to discover a class’s properties.