Skip to main content

Plain Old Java Object (POJO) Explained

Plain Old Java Object or POJO is just an ordinary Java object, The term was originally coined by Martin Fowler, Rebecca Parsons, and Josh Mackenzie in September 2000.

According to Martin Fowler

The term was coined while Rebecca Parsons, Josh MacKenzie and I were preparing for a talk at a conference in September 2000. In the talk we were pointing out the many benefits of encoding business logic into regular java objects rather than using Entity Beans. 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.

Generally, a POJO is not bound to any restriction and any Java object can be called a POJO but there are some directions. A well-defined POJO should follow below directions.
  1. Each variable in a POJO should be declared as private.
  2.     private long id;
    private String value = "";
    private Collection<Term> children = Collections.emptyList();

  3. Default constructor should be overridden with public accessibility.
  4.     public Term() {
    }

  5. Each variable should have its Setter-Getter method with public accessibility.
  6.     public long getId() {
    return id;
    }

    public void setId(long id) {
    this.id = id;
    }

    public String getValue() {
    return value;
    }

    public void setValue(String value) {
    this.value = value;
    }

    public Collection<Term> getChildren() {
    return children;
    }

    public void setChildren(Collection<Term> children) {
    this.children = children;
    }
  7. Generally POJO should override equals(), hashCode() and toString() methods of Object (but it's not mandatory).

  8. Overriding these methods will help us while using our POJO with different Java collection classes or writing it directly to console or inspecting it in debugger.

        @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((value == null) ? 0 : value.hashCode());
    result = prime * result + ((children == null) ? 0 : children.hashCode());
    return result;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    Term other = (Term) obj;

    if (value == null) {
    if (other.value != null)
    return false;
    } else if (!value.equals(other.value))
    return false;

    if (children == null) {
    if (other.children != null)
    return false;
    } else if (!children.equals(other.children))
    return false;

    return true;
    }

    @Override
    public String toString() {
    return "Term [id=" + id + ", value=" + value + ", children=" + children + "]";
    }

  9. Overriding compare() method of Comparable interface used for sorting (Preferable but not mandatory).
  10.     @Override
    public int compareTo(Term term) {
    return (int) (this.id - term.id);
    }

And according to Java Language Specification, a POJO should not have to
  1. Extend prespecified classes, as in
  2.     public class Foo extends javax.servlet.http.HttpServlet {

    }

  3. Implement prespecified interfaces, as in
  4.     public class Bar implements javax.ejb.EntityBean {

    }

  5. Contain prespecified annotations, as in
  6.     @javax.persistence.Entity 
    public class Baz {

    }

However, developers and frameworks describe a POJO still requires the use prespecified annotations to implement features like persistence, declarative transaction management etc. So the idea is that if the object was a POJO before any annotations were added would return to POJO status if the annotations are removed then it can still be considered a POJO.
A JavaBean is a special kind of 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.

Comments

Post a Comment

Popular posts from this blog

What is Variable Shadowing and Hiding in Java

Java allows us to declare a variable whenever we need it, We can categorize all our variables into 3 categories which have different-different scopes Instance Variables - Defined inside a class and have object level scope. Class Variables - Defined inside a class with static keyword, have class level scope common to all objects of the same class Local Variables - Defined inside a method or in any conditional block, have the block-level scope and only accessible in the block where it defined. What is Variable Shadowing Variable shadowing happens when we define a variable in a closure scope with a variable name and we have already defined a variable in outer scope with the same name. In other words, when a local variable has the same name as one of the instance variable, the local variable shadows the instance variable inside the method block. In the following example, there is an instance variable named x and inside method printLocalVariable(), we are shadowing it by the local ...

Why String is Immutable and Final in Java

While coding in any programming language we always require some predefined types which we can use to write the code and every programming language provides these types in its way e.g. Java provides primitive types ( int, long, char float etc) and reference types (custom types like Object, String, Thread ). For string manipulation, Java provides a class java.lang.String which gives us a way to create string objects and provides different behaviors to operate on those objects e.g. replace(), length() String name = "Naresh"; System.out.print(name.length()); System.out.print(name.isEmpty()); Whenever we talk about String class in Java we say it is i mmutable in nature and all string literals are stored in String Constant Pool (SCP) . Prior to Java 7 String Constant Pool belongs to Permanent Generation area of heap which means Garbage Collector will not touch it in normal scenarios. But from Java 7 onwards string constant pool is not part of Perm Gen but live with o...

Creating objects through Reflection in Java with Example

In Java, we generally create objects using the new keyword or we use some DI framework e.g. Spring to create an object which internally use Java Reflection API to do so. In this Article, we are going to study the reflective ways to create objects. There are two methods present in Reflection API which we can use to create objects Class.newInstance() → Inside java.lang package Constructor.newInstance() → Inside java.lang.reflect package However there are total 5 ways create objects in Java, if you are not aware of them please go through this article 5 Different ways to create objects in Java with Example . Both Class.newInstance() and java.lang.reflect.Constructor.newInstance() are known as reflective methods because these two uses reflection API to create the object. Both are not static and we can call earlier one on a class level object while latter one needs constructor level object which we can get by using the class level object. Class.newInstance() The Class class is th...