POJO stands for Plain Old Java Object. In simple terms, we use POJO to make a programming model for declaring object entities. The classes are simple to use and do not have any restrictions as compared to Java Beans.
To read about POJO classes and Java Bean refer to the following article – POJO classes and Java Bean
POJO is handier as its best in readability and reusability. The only difference between java bean and POJO classes is POJOs don’t have any restrictions other than java but java beans have.
Properties of POJO classes
- The POJO classes must be public so that we can use them outside the class.
- POJO does not have any name convention for properties and methods. It’s just a java class consisting of some variables and getters-setters.
- POJO classes are used in hibernate for mapping to database objects. That means all object entities we make in POJO classes will be reflected in a database object.
- It should not extend classes, implement interfaces, or contain prespecified annotations. Also, it does not bound with any restrictions but strictly follows java language specifications.
- In hibernate, POJOs also contain some annotations like @Entity, @Table, @id, etc for avoiding XML files for database objects.
Working with POJO classes
POJO classes are used to encapsulate business logic and its members are treated as a database entity. The main motive of POJO classes is to define an object entity for hibernating.
Implementation:
Let us make an employee POJO class
A. File: Employee.java
Java
import java.io.*;
class Employee {
private int empid;
private String name;
private int age;
public int getEmpid() { return empid; }
public void setEmpid( int empid) { this .empid = empid; }
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public int getAge() { return age; }
public void setAge( int age) { this .age = age; }
}
|
Let’s make a POJO class method and use them to set and get the data.
B. File: MainClass.java
Java
import java.io.*;
class MainClass {
public static void main(String[] args)
{
employee emp = new employee();
emp.setEmpid( 123 );
emp.setName( "Geek" );
emp.setAge( 21 );
System.out.println( "The employee ID is "
+ emp.getEmpid());
System.out.println( "The name of the employee is "
+ emp.getName());
System.out.println( "The age of the employee is "
+ emp.getAge());
}
}
|
Output:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
28 Feb, 2022
Like Article
Save Article