A POJO (Plain Old Java Object) is a simple Java class that is not tied to any special frameworks or restrictions. It is mainly used to represent data.
Key Features of a POJO:
Private variables (fields)
Public getter and setter methods
No dependency on external frameworks
May include constructors
Example:
class Student {
// Private variables
private int id;
private String name;
// Setter methods
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
// Getter methods
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
// Object creation
Student s = new Student();
// Setting values
s.setId(101);
s.setName("Sam");
// Getting values
System.out.println("Id : " + s.getId());
System.out.println("Name : " + s.getName());
}
}
Output:
Why POJO is Used:
POJO classes are mainly used to:
Store data
Transfer data between layers
Improve code readability
Make programs simple and reusable
Advantages of POJO:
Easy to create
Easy to understand
Reusable
Simple maintenance
Better readability

Top comments (0)