POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program. POJOs have gained the most acceptance because they are easy to write and understand.
A POJO should not:
- Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { … } is not a POJO class.
- Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { … } is not a POJO class.
- Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { … } is not a POJO class.
Example:
// Employee POJO class to represent entity Employee
public class Employee
{
// default field
String name;
// public field
public String id;
// private salary
private double salary;
//arg-constructor to initialize fields
public Employee(String name, String id,
double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
// getter method for name
public String getName()
{
return name;
}
// getter method for id
public String getId()
{
return id;
}
// getter method for salary
public Double getSalary()
{
return salary;
}
}