What is POJO Class in Java ?

What is POJO Class in Java ?

  • POJO stands for Plain Old Java Object. It is a term used to describe a Java class that follows simple conventions and does not depend on any specific frameworks or libraries.

  • A POJO typically contains private fields with corresponding getter and setter methods, and it may also include additional methods for behaviour.

  • POJOs are commonly used to encapsulate data and represent entities in an application. They are often used in frameworks like Java Persistence API (JPA) for mapping database records to Java objects, or in serialization/deserialization processes where objects are converted to and from formats like JSON or XML.

  • The key characteristic of a POJO is its simplicity and lack of dependencies on specific frameworks or technologies, making it easier to understand, test, and maintain.

Key characteristics of a POJO class include:

1. Attributes (Fields):

  • A POJO class typically contains private fields that represent the attributes or properties of an object. These fields are kept private to encapsulate the internal state of the object.
public class Person {
    private String name;
    private int age;
}

2. Constructors:

  • POJO classes often have one or more constructors. A default constructor with no parameters and additional constructors with parameters can be provided for flexibility in object instantiation.
public Person() {
    // Default constructor
}

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

3. Getter and Setter Methods:

  • Private fields are accessed and modified through public getter and setter methods. This follows the principle of encapsulation, allowing controlled access to the class's state.
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;
}

4. Encapsulation:

  • The private fields and the public getter/setter methods ensure encapsulation. Encapsulation restricts direct access to the internal state of an object, promoting data integrity and code maintainability.

5. No Business Logic:

  • POJOs typically do not contain complex business logic. They are meant to represent data structures rather than implement extensive functionality.

6. Serializable (Optional):

  • Depending on the use case, a POJO class may implement the Serializable interface. This allows instances of the class to be serialized (converted to byte streams) for various purposes like storage or network transmission.
public class Person implements Serializable {
    // ... (fields, constructors, methods)
}

7. No Dependency on Frameworks:

  • True to its name, a POJO should not have dependencies on external frameworks or libraries. It adheres to standard Java practices, making it portable and versatile.

8. Testing and Mocking:

  • POJOs are easily testable since they do not have dependencies on external frameworks or services. This makes it simpler to write unit tests and mock the POJO’s behaviour as needed.

Working of POJO Class:

An object class in Java is the POJO class. It includes business logic. In a Model View Controller architecture, after viewing, the controller would communicate with the business logic, which would then get in touch with the Java POJO class to retrieve the data. The phrase "business logic" describes the comprehensive collection of guidelines that specify how data will be handled or stored inside a business or application domain.

Below is the working of the POJO class.

The POJO class contains the same members as the database object in the example above because the database entity serves as a representation of the POJO.

Benefits of POJOs:

  • Simplicity: Easy to understand and create, promoting clean code.

  • Reusability: POJOs can be used across different parts of your application or even in other projects.

  • Flexibility: Not constrained by specific frameworks, allowing for wider integration.

  • Testability: POJOs often have simpler dependencies, making them easier to test in isolation.

Use of POJO Class in Java:

The POJO class was developed so that Java programs may utilize the objects. The main benefit of the POJO class is that it eliminates the need to repeatedly generate objects in other Java programs. Simply said, we may use the get() and set() methods to access the objects.

The steps listed below can be used to access objects from the POJO class:

  • Assemble POJO classes for things

  • Utilize the set() function to set the values.

  • Use the get() function to retrieve the values.

  • Representing data models (e.g., user information, product details)

  • Transferring data between different parts of an application

  • Persisting data to databases or other storage systems

Example:

public class Student {
    // Private fields (attributes)
    private int studentId;
    private String firstName;
    private String lastName;
    private int age;

    // Constructors
    public Student() {
        // Default constructor
    }

    public Student(int studentId, String firstName, String lastName, int age) {
        this.studentId = studentId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    // Getter and Setter methods
    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // toString method for easy debugging
    @Override
    public String toString() {
        return "Student{" +
                "studentId=" + studentId +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", age=" + age +
                '}';
    }
}

In this example:

  • The Student class has private fields (studentId, firstName, lastName, and age) representing attributes of a student.

  • It provides two constructors: a default constructor and a parameterized constructor for creating instances of the class with specified values.

  • Getter and setter methods are provided for each private field, allowing controlled access to the class's state.

  • The toString method is overridden to provide a string representation of the Student object, which is helpful for debugging.

This Student class serves as a basic example of a POJO class, following the conventions of simplicity, encapsulation, and adherence to standard Java practices. It can be used to represent a student entity in various applications.