POJO Class in Java
The abbreviation for POJO is Plain Old Java Object. It is a normal object with no special restrictions. The file does not need a special classpath. Due to the absence of any kind of framework, any Java program can make use of this concept. Developers can create it easier to implement readability and reusability in the code.
What is Pojo in Java?
The term POJO was coined by Martin Fowler who was an American software developer in the year 2000. The Sun microsystem has been providing this class since EJB 3.0. Plain Old Java Objects or POJO refers to objects that do not have any sort of restrictions. These restrictions include special conventions for access modifiers, implementing serializable interfaces, etc. Thus, they are known as free objects.
Due to high readability and reusability, this concept is widespread over the years. This class also does not have any rules for naming conventions and hence it is easier to read and write. It also has no dependencies on any libraries, annotations, or interfaces. Therefore, multiple project types like web, console, desktop, etc use it there is no need to install packages.
POJO consists of getter and setter methods. These methods play a vital role in retrieving and modifying the value of a variable that is present outside the encapsulating class. Normally, a getter obtains the variable’s value and the setter modifies the variable’s value.
Characteristics of a POJO class in Java:
Listed below are some of the characteristics of a POJO class in Java.
1. The only access modifier that a POJO class follows is that it is always public.
2. This class should always have a public default constructor.
3. It contains the public Getter and Setter methods, this lets the other Java program access those values.
4. This class may or may not contain a constructor with arguments.
5. The objects in a POJO class can be public, private, or protected.
6. It should not extend predefined classes.
7. It should not use predefined interfaces or annotations.
8. The instance variables for the objects in a POJO class should only be private. These are variables that are
9. declared in a class but outside of the block, methods, or constructor.
Rules for Pojo Class
As we have seen the properties of a POJO class, now let us look into what a POJO class should not consist of:
1. A POJO class should not extend prespecified or predefined classes.
For example:
public class DataFlairEx extends javax.servlet.http.HttpServlet {…} is not a POJO class.
2. This class should not implement predefined or pre-specified interfaces.
For example:
public class Bar implements javax.ejb.EntityBean {…} is not a POJO class.
3. It should also not consist of any predefined notations.
For example:
@javax.presistence.Entity public class Baz {…} is not a POJO class.
Make sure that you avoid these while using a POJO class.
Working of a POJO class:
The POJO class encapsulates business logic. In a Model-View-Controller or an MVC architecture, the Controller cooperates with the business logic that contacts the POJO class to access the data.
The diagram given below explains the working of the POJO class:
Using POJO class in a Java Program:
To create a POJO class, we use the objects in the other Java programs. This object need not be created each time when required. We can access the objects using the get() and set() methods.
Follow the below steps to access the objects from a POJO class:
1. Create an object for the POJO class
2. Set the values using the set() method.
3. Obtain the values using the get() method.
Example program:
// POJO class sample program FirstCodeEmp.java: package Jtp.PojoDemo; public class FirstCodeEmp{ private String name; private String id; private double salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getSal() { return salary; } public void setSal(double salary) { this.salary = salary; } } // MyClass.java //Using POJO class objects in the MyClass Java program package Jtp.PojoDemo; public class MyClass { public static void main(String[] args) { Employee e= new Employee(); e.setName("Catherine"); // Setting the values using the set() method e.setId("DFE08"); e.setSal(400000); System.out.println("Name: "+ obj.getName()); System.out.println("Id: " + obj.getId()); System.out.println("Salary: " +obj.getSal()); } }
Output:
Name: Catherine
Id: DFE08
Salary: 400000.0
In the above program, we have accessed the POJO class properties in the MyClass.java program.
Conclusion
Using the POJO class, we can make the code a lot less complicated. This helps a lot in testing and flexibility of the code. I hope you were able to grasp a lot about the POJO class in Java from this article.