If you’ve programmed in Java for any amount of time, you’ll inevitably come across the terms bean, JavaBean, and POJO. What are they and how do they differ? Let’s start with some definitions:
According to Wikipedia, “JavaBeans are classes that encapsulate many objects into a single object (the bean).” Essentially, it’s a class with three characteristics:
- it’s serializable (i.e. implements java.io.Serializable)
- it has a no-argument constructor
- it allows access to its properties through the standard getters and setters.
A bean is a JavaBeans component, which is a reusable software component for Java. While I’m not 100% sure, it seems that “JavaBean” and “bean” are interchangeable.
POJO stands for Plain Old Java Object. Essentially, it’s the one of the simplest types of objects possible. There are no annotations, it extends no other classes, and implements no interfaces except perhaps a marker interface, such as java.io.Serializable. Finally, just like a JavaBean, it has a no-argument constructor, and allows access to its properties through the standard getters and setters.
I like the following statement from StackOverflow:
All JavaBeans are POJOs but not all POJOs are JavaBeans.
Some people say a POJOs can’t implement java.io.Serializable, which would mean a JavaBean is not a POJO. I would argue that a POJO can implement java.io.Serializable, since implementing marker interfaces do not corrupt the “plainness” of a POJO.