POJO Class in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

POJO, short for Plain Old Java Object, is a standard Java object with minimal restrictions imposed by the Java Language Specification. It operates without requiring any specific classpath. These objects serve as intermediaries for communication between different layers or components within distributed systems, encapsulating data and related behavior. Typically, POJOs feature private member variables alongside public getter and setter methods, with additional methods for data manipulation if needed.

Example

Explanation of the example:

In the above example, we can observe that there are no restrictions on the access modifiers for the fields; they could be declared as public, private, or protected. Also, it is not mandatory to include any constructor in it. Thus, we can observe that the above example is a classic case of the POJO class in Java.

Properties of POJO

The following are some essential properties of the POJO class in Java:

  • The POJO class in Java should always be public.
  • The POJO class in Java should always have a public default constructor.
  • The POJO class in Java may contain the arguments constructor.
  • The objects of the POJO class in Java should contain public Getters and Setters so that other Java programs can access object values.
  • The access modifiers for the objects in the POJO class of java could be public, private, or protected.
  • The instance variables for the objects in the POJO class of Java should be private for better security.
  • The POJO class in Java must not extend predefined classes.
  • The POJO class in Java must not implement prespecified interfaces.
  • The POJO class in Java must not have any prespecified annotation.

Working of the POJO Class

The POJO class in Java is an object class. It encapsulates the Business logic. Upon viewing in a Model View Controller architecture, the controller would interact with the business logic, which would contact the POJO class in Java to access the data. Business logic refers to the overall set of rules determining how the data will be stored or manipulated in a business or application domain.

Below is the pictorial representation of the working of the POJO class in Java:

pojo class working

In the above example, the database entity represents the POJO, i.e. the POJO class has the same members as the database entity.

Using POJO Class in Java Program

To use a POJO class in a Java program, you need to follow the below steps:

  • Create a POJO class: Define a POJO class with private fields, public getters, and setters.
  • Instantiate the POJO class: Create an object of the POJO class using the new operator.
  • Set values: Set the values of the POJO class fields using the setter methods.
  • Get values: Retrieve the values of the POJO class fields using the getter methods.

Here is an example of how to use a POJO class in a Java program:

Output:

In the above example, we have created a POJO class named Person with private fields name and age and provided public getter and setter methods for these fields.

In the main method of the Main class, we created an object of the Person class using the new operator. We then set the values of the name and age fields using the setter methods. Finally, we retrieved the values of these fields using the getter methods and printed them on the console.

Java Bean Class

JavaBeans are classes that encapsulate many objects into a single object. JavaBeans share similarities with POJO classes in Java, as both can be used to define objects, providing better reusability and increased readability. However, JavaBeans have some conventions or restrictions imposed upon them, such as the requirement for a default no-argument constructor and the use of standard getter and setter methods for access to the bean's properties. In contrast, POJO classes in Java do not have any remarkable restrictions imposed upon them.

The JavaBeans need to follow the following conventions:

  • They should always implement Serializable interface.
  • They must have a public no-arg constructor.
  • The properties within them should always be private with public getters and setter methods.

Example:

POJO vs Bean

POJOJava Bean
The POJO class does not have any special restrictions.Java Bean is a kind of POJO with special restrictions.
The POJO class does not impart substantial control over the members.The Java Beans do have complete control over the members.
The POJO class can be used to implement serializable interfaces.The Java Beans must implement serializable interfaces.
The POJO class fields can be accessed with their names.The Java Beans fields can be accessed only with the help of getters and setters.
The POJO class fields can be public, private, or protected.The Java Bean class fields should be private.
The POJO class does not necessarily need to have a no-arg constructor.The Java Bean class should have a no-arg constructor.
The POJO class in Java helps us set up a user environment without any restrictions on our members and with full authority over our entity.The Java Beans can partially provide our entity to the users.

Conclusion

  • POJO is a regular Java object with no special restrictions.
  • The POJO class in java is used to implement readability & reusability.
  • The POJO class fields can be public, private, or protected.
  • JavaBeans are classes that encapsulate many objects into a single object.
  • JavaBeans are a subset of POJOs that follow specific conventions for naming, constructing, and modifying their properties.