DEV Community

PRIYA K
PRIYA K

Posted on

Pojo Class in Java

POJO class in Java
POJO stands for Plain Old Java Object.
It means a simple Java class that is used to store data.
A POJO class is a simple Java class used to store and transfer data through objects.

A POJO class usually contains:
private variables (fields)
getter methods
setter methods
constructor (optional)
It does not need special inheritance or framework-specific code.

Simple Example

class Student {
    private String name;
    private int age;

    public Student() {
    }

    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;
    }
}
Using the POJO class
public class Main {
    public static void main(String[] args) {

        Student s = new Student();

        s.setName("Priya");
        s.setAge(21);

        System.out.println(s.getName());
        System.out.println(s.getAge());
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
Priya
21

Why use POJO class?
A POJO is mainly used to represent real data objects.

Example:
Student details
Employee details
Bank account details

Instead of keeping many separate variables, we group related data into one object.

Why not just use normal variables?
Without POJO:
String name = "Priya";
int age = 21;

This works for one person, but for many students it becomes messy.

With POJO:
Student s1 = new Student();
Student s2 = new Student();

Each object can store separate data.

Main features of POJO
Simple class
No special restrictions
Used for storing data
Reusable
Easy to maintain

Top comments (0)