Sobes.tech
Junior

What is a POJO class?

sobes.tech AI

Answer from AI

POJO (Plain Old Java Object) is a simple Java object. A POJO class does not inherit from other specific Java classes (except Object), does not implement special interfaces, and does not use framework-specific annotations.

It typically contains:

  • Private fields.
  • Public getters and setters for accessing the fields.
  • A no-argument constructor (default constructor) or other constructors.

Example of a POJO class:

public class User {
    private String name;
    private int age;

    public User() {
        // Default constructor
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }
}

POJOs promote loose coupling in applications, simplify testing and integration with various frameworks (such as Spring, Hibernate), which can automatically create and populate such objects.