70

Recently I've started hearing about "POJOs" (Plain Old Java Objects). I googled it, but still don't understand the concept well. Can anyone give me a clear description of a POJO?

Consider a class "Person" with variables "id, name, address, salary" -- how would I create a POJO for this scenario? Is the code below a POJO?

public class Person {
    //variables
    People people = new People();
    private int id;
    private String name;
    private String address;
    private int salary;


    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public int getSalary() {
        return salary;
    }

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

    public void setName() {
        this.name = name;
    }

    public void setAddress() {
        this.address = address;
    }

    public void setSalary() {
        this.salary = salary;
    }
}
randers
  • 5,031
  • 5
  • 37
  • 64
  • 1
    check out my answer to a similar question: http://stackoverflow.com/questions/3392580/a-better-explanation-of-poco/3392625#3392625. This is referring to POCO's (.NET version of POJOS), but the fundamentals are the same. – RPM1984 Aug 20 '10 at 00:22
  • @Tamil can you explain once again to me. about pojo – Poovizhirajan N Jul 22 '14 at 07:19
  • @Poovizhirajan.N, sorry for the long delay. somehow missed it. –  Mar 24 '15 at 06:28

11 Answers11

83

A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:

  1. Default no-arg constructor
  2. Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
  3. Must implement java.io.Serializable

POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.

The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.

Martin Fowler coined a new acronym.

Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.

Here's an example that you can wrap your head around:

public class MyFirstPojo
{
    private String name;

    public static void main(String [] args)
    {
       for (String arg : args)
       {
          MyFirstPojo pojo = new MyFirstPojo(arg);  // Here's how you create a POJO
          System.out.println(pojo); 
       }
    }

    public MyFirstPojo(String name)
    {    
        this.name = name;
    }

    public String getName() { return this.name; } 

    public String toString() { return this.name; } 
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I ll never going to have this doubt again. Thanks. –  Aug 20 '10 at 00:42
  • 16
    Some people can understand practical demo's faster, than reading. I am one of them. –  Aug 20 '10 at 00:50
  • Your particular example is immutable, but others are mutable. Did you just happen to make yours immutable? Is there any distinction between POJOs that contain unshared references to mutable objects for the purpose of encapsulating their state, and those which contain references identifying mutable objects to which other references also exist, for the purpose of encapsulating their identity? – supercat Dec 06 '13 at 18:19
  • Just happened to come out that way. I was not intending to be exhaustive. It's a simple example, nothing more. – duffymo Dec 06 '13 at 18:38
  • @duffymo sorry if I am wrong, Is this violating the no args rule/instruction? – diyoda_ Feb 23 '14 at 14:06
  • 7
    I think I stated it in my answer: requiring a no-arg constructor is a Java Bean requirement, but not so for POJO. Besides, *any* rule can be broken. Know the rules; know the exceptions; understand the consequences. This is software development, not murder court. – duffymo Feb 23 '14 at 14:14
  • @duffymo: After 5 years, thank you for the answer. People in SO helped me a lot more than anyone. And, personally I like your last comment. –  Nov 30 '15 at 18:25
  • So kind of you to say. – duffymo Nov 30 '15 at 18:27
  • @duffymo, as you mentioned, **Must implement java.io.Serializable**, but why did not you write an implementation on `MyFirstPojo`? I would like to know that is it "must do" thing or not? – jundev Aug 02 '18 at 20:24
  • 1
    Please read it again. The Java Bean standard mandates Serialiable; POJO does not. – duffymo Aug 02 '18 at 20:26
  • Oh yes, you have mentioned JavaBean requirements. +1 for great explanation – jundev Aug 02 '18 at 20:32
29

POJO:- POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.

Properties of POJO

  1. All properties must be public setter and getter methods
  2. All instance variables should be private
  3. Should not Extend prespecified classes.
  4. Should not Implement prespecified interfaces.
  5. Should not contain prespecified annotations.
  6. It may not have any argument constructors

Example of POJO

public class POJO {

    private String value;

    public String getValue() {
         return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
Jeremy
  • 1,894
  • 2
  • 13
  • 22
Md Azaharuddin Ali
  • 1,889
  • 17
  • 13
  • #1, #2 contradicts the definition on wikipedia. https://en.wikipedia.org/wiki/Plain_Old_Java_Object#Definition. You're describing a "JavaBean POJO" (a term which I find to be an oxymoron, but that's just me) – Nilzor Nov 22 '15 at 13:13
  • Is the term "property" appropriate? Maybe you mean method? – Valerio Bozz Mar 28 '23 at 14:03
5

A POJO is a Plain Old Java Object.

From the wikipedia article I linked to:

In computing software, POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean

Your class appears to already be a POJO.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
4

POJO class acts as a bean which is used to set and get the value.

public class Data
{


private int id;
    private String deptname;
    private String date;
    private String name;
    private String mdate;
    private String mname;

    public int getId() {
        return id;
    }

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

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMdate() {
        return mdate;
    }

    public void setMdate(String mdate) {
        this.mdate = mdate;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }
}
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Subodh Bisht
  • 859
  • 7
  • 19
  • 33
2

When you aren't doing anything to make your class particularly designed to work with a given framework, ORM, or other system that needs a special sort of class, you have a Plain Old Java Object, or POJO.

Ironically, one of the reasons for coining the term is that people were avoiding them in cases where they were sensible and some people concluded that this was because they didn't have a fancy name. Ironic, because your question demonstrates that the approach worked.

Compare the older POD "Plain Old Data" to mean a C++ class that doesn't do anything a C struct couldn't do (more or less, non-virtual members that aren't destructors or trivial constructors don't stop it being considered POD), and the newer (and more directly comparable) POCO "Plain Old CLR Object" in .NET.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
2

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. Default constructor should be overridden with public accessibility.
  3. Each variable should have its Setter-Getter method with public accessibility.
  4. Generally POJO should override equals(), hashCode() and toString() methods of Object (but it's not mandatory).
  5. Overriding compare() method of Comparable interface used for sorting (Preferable but not mandatory).

And according to Java Language Specification, a POJO should not have to

  1. Extend pre-specified classes
  2. Implement pre-specified interfaces
  3. Contain pre-specified annotations

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.

Read more on Plain Old Java Object (POJO) Explained.

Naresh Joshi
  • 4,188
  • 35
  • 45
  • But every Java class at least extends Object class which doesn't extend any other classes ? Should we add can't extend other classes except Object or can't extend any classes explicitly ? – Badri Paudel Jan 10 '21 at 06:13
  • Actually these are not hard rules and also we do not need to extend Object because it is done by default. – Naresh Joshi Jan 11 '21 at 06:45
1

there are mainly three options are possible for mapping purpose

  1. serialize
  2. XML mapping
  3. POJO mapping.(Plain Old Java Objects)

While using the pojo classes,it is easy for a developer to map with the database. POJO classes are created for database and at the same time value-objects classes are created with getter and setter methods that will easily hold the content.

So,for the purpose of mapping in between java with database, value-objects and POJO classes are implemented.

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
mohit sarsar
  • 179
  • 3
  • 8
1
import java.io.Serializable;

public class Course implements Serializable {

    protected int courseId;
    protected String courseName;
    protected String courseType;

    public Course() {
        courseName = new String();
        courseType = new String();
    }

    public Course(String courseName, String courseType) {
        this.courseName = courseName;
        this.courseType = courseType;
    }

    public Course(int courseId, String courseName, String courseType) {
        this.courseId = courseId;
        this.courseName = courseName;
        this.courseType = courseType;
    }

    public int getCourseId() {
        return courseId;
    }

    public void setCourseId(int courseId) {
        this.courseId = courseId;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseType() {
        return courseType;
    }

    public void setCourseType(String courseType) {
        this.courseType = courseType;
    }

    @Override
    public int hashCode() {
        return courseId;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null || obj instanceof Course) {
            Course c = (Course) obj;
            if (courseId == c.courseId && courseName.equals(c.courseName)
                    && courseType.equals(c.courseType))
                return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Course[" + courseId + "," + courseName + "," + courseType + "]";
    }
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0
public class UserInfo {
        String LoginId;
        String Password;
        String FirstName;
        String LastName;
        String Email;
        String Mobile;
        String Address;
        String DOB;

        public String getLoginId() {
            return LoginId;
        }

        public void setLoginId(String loginId) {
            LoginId = loginId;
        }

        public String getPassword() {
            return Password;
        }

        public void setPassword(String password) {
            Password = password;
        }

        public String getFirstName() {
            return FirstName;
        }

        public void setFirstName(String firstName) {
            FirstName = firstName;
        }

        public String getLastName() {
            return LastName;
        }

        public void setLastName(String lastName) {
            LastName = lastName;
        }

        public String getEmail() {
            return Email;
        }

        public void setEmail(String email) {
            Email = email;
        }

        public String getMobile() {
            return Mobile;
        }

        public void setMobile(String mobile) {
            Mobile = mobile;
        }

        public String getAddress() {
            return Address;
        }

        public void setAddress(String address) {
            Address = address;
        }

        public String getDOB() {
            return DOB;
        }

        public void setDOB(String DOB) {
            this.DOB = DOB;
        }
    }
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0
  1. File-setting-plugins-Browse repositories
  2. Search RoboPOJOGenerator and install, Restart Android studio
  3. Open Project and right click on package select on Generate POJO from JSON
  4. Paste JSON in dialogbox and select option according your requirements
  5. Click on Generate button
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

If a class is not bogged down from a framework or a library, then an object created from that class is recognized as a POJO.

Let's see some examples:

class MyServlet extends HttpServlet{
    //....
}

The sole meaning of MyServlet class is given by the HttpServlet class. Therefore the objects created from the MyServlet are not POJOs.

class MyClass implements Serializable{
    //...
}

The Serializable interface does not give a meaning to the class MyClass. Therefore the objects created from the MyClass are POJOs.