Last updated on August 5th, 2024
A Plain Old Java Object is called the POJO class. POJO class has some points:
- The Java class without any specialities i.e. is a regular Java class.
- While developing this class we need not follow any serious rules.
- The Java class that does not extend from the technology or framework API class and does not implement technology or framework API interfaces is called the POJO class.
- Use of this class is improving the readability and reusability of the code.
- There is no restriction of modifiers of the properties in this class.
- This class is not mandatory for the 0-param constructor.
- This class uses getter() and setter() methods for transmitting and retrieving data from one layer to another in a real-time project.
- The POJO class should always be public.
- The POJO class should have the default constructor.
- The POJO class may contain the parameterized constructor if needed.
- Every Java Bean class is a POJO class.
Example of POJO class in Java
Some of the following examples of POJO classes in Java are mentioned below:
Example 1
class Test{
//your statement
}
- It is a POJO class.
Example 2
class Test implements Serializable {
//your statement
}
- It is also a POJO class.
Example 3
class Test extends Demo {
//your statement
}
class Demo {
//your statement
}
- Test, Demo classes are POJO classes.
Example 4
class Test extends Thread {
//your statement
}
- Test is a POJO class.
Example 5
class Test extends HttpServlet {
//your statement
}
- Test is not a POJO class.
Example 6
class Test extends java.sql.Connection {
//your statement
}
- Test is not a POJO class.
Creating a POJO class in Java
//Person POJO class represents properties of the Person
public class Person{
//default property
Long id;
//public property
public String name;
//private property
private Integer mobileNo;
//parameterized constructor to initialize properties
public Person(Long id,String name,Integer mobileNo){
this.id=id;
this.name=name;
this.mobileNo=mobileNo;
}
//getter of the id property
public Long getId() {
return id;
}
//setter of the id property
public void setId(Long id) {
this.id = id;
}
//getter of the name property
public String getName() {
return name;
}
//setter of the name property
public void setName(String name) {
this.name = name;
}
//getter of the mobileNo property
public Integer getMobileNo() {
return mobileNo;
}
//getter of the mobileNo property
public void setMobileNo(Integer mobileNo) {
this.mobileNo = mobileNo;
}
}
- In the above example we created a POJO class with the name “Person”. Properties of this class id, name, and mobileNo and modifiers of these properties are default, public and private respectively. This class contains a parameterized constructor to initialize these properties. This class also has the setter and getter method for each property for sending and retrieving data from classes. This is an example of a POJO class that uses every property in this Person class to make a POJO class.
- We can use the POJO class as a Model class when working on a real-time project. At that time we are sending data from Controller to Service to DAO and vice versa. POJO class helps to use reusability features in the code in real-world scenarios.
- We can use a private modifier for the properties of the POJO class when working on real-time projects.
Conclusion
This topic is explained What is a Pojo class? How can we identify which is the POJO class?