tags:

views:

78

answers:

3

Recently i start hearing about POJO objects. I googleg it. I cant understand what they told in there. Can, anyone describe me about POJO? or

Consider a class "Person" with variables "id, name, address, salary" and a class "People". How to create POJO object for this scenario.

For your convenience, i have added some base.

 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;
}
}
A: 

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
Hey, i also came across thro wiki. But i need a practical explanation man...
NooBDevelopeR
A: 

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
Thankx for your reply senior. But i want some practical explanation.
NooBDevelopeR
You want a practical explanation of nomenclature for "not special"? Really?
Jon Hanna
+4  A: 

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
I ll never going to have this doubt again. Thanks.
NooBDevelopeR
Some people can understand practical demo's faster, than reading. I am one of them.
NooBDevelopeR
I understand. I can be that way, too. I'll edit my answer.
duffymo