Java supports Object Oriented Programming System (OOPs) which is a programming paradigm. From the full form of OOPs , we can understand that it is a programming approach which is oriented around something called “Object” and an Object can contain data (Member variables ) and mechanism (member functions) to manipulate on these data.
For an example – A company has many employees and there are some basic details which need to be stored in such a way that it can be accessed and manipulated easily. If we create a blue print of an Employee which defines what are data to be stored and mechanisms to manipulate on those data then our problem can be solved.
We can create a Class “Employee” ( We can give any meaningful name) and to store employee related data we can declare fields or variables in this class. We also need to retrieve and manipulate data so we need to define some mechanism or methods to do so.
package PojoPayloads;
public class Employee {
// fields to store data
public String firstName;
public String lastName;
public double salary;
public String cityName;
public boolean isMarried;
public char gender;
// Create employees with different data
public Employee(String firstName, String lastName, double salary, String cityName, boolean isMarried,
char gender) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.cityName = cityName;
this.isMarried = isMarried;
this.gender = gender;
}
// Public getter setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public boolean isMarried() {
return isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
Now you can store all employees details in separate objects using same blueprint or class.
public class EmployeeDetails {
public static void main(String[] args) {
Employee amod = new Employee("Amod", "Mahajan", 100000, "Benagluru", false, 'M');
Employee animesh = new Employee("Animesh", "Prashant", 200000, "Benagluru", true, 'M');
// Get married status of Amod
System.out.println("Is Amod married? : "+amod.isMarried());
// Amod is married now and change data
amod.setMarried(true);
System.out.println("Is Amod married now ? : "+amod.isMarried());
// Increase salary of animesh
animesh.setSalary(500000);
System.out.println("Updated salary of animesh : "+ animesh.getSalary());
}
}
I have written very simple business logic to work on data above. You can do however you want to do. For example :- Hike salary by percentage rather than setting a value.
In above program , “amod” and “animesh” are ordinary Java objects or POJO.
POJO stands for Plain Old Java Object and it is an ordinary Java objects not a special kind of. The term POJO was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000 when they were talking on many benefits of encoding business logic into regular java objects rather than using Entity Beans or JavaBeans.