Reference articles on history, science, culture and more
Encyclopedia

Plain old Java object

Type of object in Java programming language

In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction. The term was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000:

We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely.

The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks. It has since gained adoption as a language-agnostic term, because of the need for a common and easily understood term that contrasts with complicated object frameworks.

The term continues an acronym pattern to coin retronyms for constructs that do not use fancy new features:

01Definition

Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification; i.e. a POJO should not have to:

  • Extend prespecified classes, as in
import javax.servlet.http.HttpServlet; public class Foo extends HttpServlet { // ... }
  • Implement prespecified interfaces, as in
import javax.ejb.EntityBean; public class Bar implements EntityBean { // ... } import javax.persistence.Entity; @Entity public class Baz { // ... }

However, many software products or frameworks that require annotations for features like persistence still consider an object (or rather a class) a POJO if it uses annotations.

02Contextual variations

JavaBeans

A JavaBean is a POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention. Because of this convention, simple declarative references can be made to the properties of arbitrary JavaBeans. Code using such a declarative reference does not have to know anything about the type of the bean, and the bean can be used with many frameworks without these frameworks having to know the exact type of the bean. The JavaBeans specification, if fully implemented, slightly breaks the POJO model as the class must implement the Serializable interface to be a true JavaBean. Many POJO classes still called JavaBeans do not meet this requirement. Since Serializable is a marker (method-less) interface, this is not much of a burden.

The following shows an example of a JavaServer Faces (JSF) component having a bidirectional binding to a POJO's property:

<h:inputText value="#{MyBean.someProperty}"/>

The definition of the POJO can be as follows:

public class MyBean { private String someProperty; public String getSomeProperty() { return someProperty; } public void setSomeProperty(String someProperty) { this.someProperty = someProperty; } }

Because of the JavaBean naming conventions the single "someProperty" reference can be automatically translated to the "getSomeProperty()" (or "isSomeProperty()" if the property is of Boolean type) method for getting a value, and to the "setSomeProperty(String)" method for setting a value.

The Project Lombok library allows to change the code dynamically to integrate those conventions without the hassle to write them. The following code would generate the same bean, with the addition of an empty constructor:

@NoArgsConstructor public class MyBean { @Getter @Setter private String someProperty; }

Other libraries or framework generate code (or bytecode) with those conventions directly. The addition of those tools help alleviate the boilerplate, which in turn reduces the bugs frequency and maintenance cost .

Transparently adding services

As designs using POJOs have become more commonly used, systems have arisen that give POJOs the full functionality used in frameworks and more choice about which areas of functionality are actually needed. In this model, the programmer creates nothing more than a POJO. This POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. Aspect-oriented programming (AOP) frameworks then transparently add cross-cutting concerns like persistence, transactions, security, and so on.

Spring was an early implementation of this idea and one of the driving forces behind popularizing this model.

An example of an EJB bean being a POJO:

The following shows a fully functional EJB bean, demonstrating how EJB3 leverages the POJO model:

public class HelloWorldService { public String sayHello() { return "Hello, world!"; } }

As given, the bean does not need to extend any EJB class or implement any EJB interface and also does not need to contain any EJB annotations. Instead, the programmer declares in an external XML file which EJB services should be added to the bean:

<enterprise-beans> <session> <ejb-name>helloWorld</ejb-name> <ejb-class>com.example.HelloWorldService</ejb-class> <session-type>stateless</session-type> </session> </enterprise-beans>

In practice, some people find annotations elegant, while they see XML as verbose, ugly and hard to maintain, yet others find annotations pollute the POJO model.

Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead of or in addition to XML. The following shows the same EJB bean as shown above but with an annotation added. In this case the XML file is no longer needed:

@Stateless public class HelloWorldService { public String sayHello() { return "Hello, world!"; } }

With the annotation as given above the bean isn't a truly pure POJO anymore, but since annotations are merely passive metadata this has far fewer harmful drawbacks compared to the invasiveness of having to extend classes and/or implement interfaces. Accordingly, the programming model is still very much like the pure POJO model.

Watch videos about Plain old Java objectExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Plain old Java object, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.