Plain Old Java Object (POJO)

by | Jan 6, 2023 | Java

Introduction

POJO in Java Language stands for Plain Old Java Object. As the name suggests, this is a normal object of Java where no special conditions or restrictions have been imposed. A plain old java object requires no special classpath. Like normal classes, a Java program enhances the reusability and readability of a particular program, thereby acting as a performance enhancer in general.

As mentioned before, since such objects do not require any special classpath, neither are they bounded by special conditions and restrictions. Therefore, these objects are quite frequently used in several Java programs. POJOs are extremely easy to maintain and, similarly, extremely easy to read and write on. These objects are truly free of restrictions, and no naming convention rules apply to the methods and properties of POJOs. They are also not tied to any particular framework. POJOs can be used for any purpose by any program in the Java domain.

History of POJO

In 2000, the term POJO was coined by an American software developer called Martin Fowler. It is available in Java from EJB 3.0 via Sun Microsystems.

The framework of a POJO class is quite simple. They contain the fields of the class, the setter methods that take value into the class, and the getter methods that retrieve values from the class.

POJO classes hold a close resemblance to Java Beans as both these entities are used to create and instantiate objects that enhance the readability and usability of the code. Beans differ from POJOs in only one way there are absolutely no restrictions on how POJOs can be used. However, some restrictions need to be followed when using Beans.

Example

In this example, we will be defining an Employee POJO class to define its objects after creation.

Sample Code

//Java program to illustrate the working of a POJO class
public class Employee
{
private String name;
private String id;
private double sal;
public String getName ()
{
return name;
}
public void setName (String name )
{
this.name = name;
}
public String getId ()
}
return id;
}
public void setId (String id)
{
this.id = id;
}
public double getSal ()
{
return sal;
}
public void setSal (double sal)
{
this.sal = sal;
}
}

 

The Employee class that has been defined in the above program is an example of a plain old java object class. If you are using Eclipse, you can easily generate the Setters and Getters methods by right-clicking on the Java program and navigating to Source -> Generate Getters and Setters. Go to the Java Program and right-click on it. You will get a list of options from where you can select Generate Getters and Setters. After selecting, you will find a general option given at the bottom of the Generate window on your screen. This will automatically help you generate the getter and the setter methods.

Properties of POJO Class

  • Since POJOs have unrestricted access and usage, therefore these classes should always be public.
  • POJO classes must have a public default constructor.
  • Argument constructors are optional for POJO classes.
  • Like the access modifier of the class, all the getter and the setter methods of the POJO class should also be public such that the values obtained by the getter methods, which have been set by the setter methods, can also be accessed by other classes.
  • The objects on a POJO class can have any of the following access modifiers such as private, public, or protected. However, to ensure better security, all the instance variables should be defined as private.
  • Predefined classes should not be extended by a POJO class.
  • POJO classes should not implement prespecified interfaces.
  • POJO classes should also not have any prespecified annotations.

Working in a POJO Class

The POJO Class is an object class that encapsulates the Business Logic. The Controller acts as the pivot in an MVC architecture as it communicates with the business logic, which contacts, in turn, the POJO class to access the data.

How to Use POJO Class in a Java Program

By creating a POJO class, you can access the objects created from other Java programs. Once you have created an object of the POJO class, you can directly call those objects in other Java programs without having to create objects of the POJO class every time you want to instantiate it. With the help of the getter and the setter methods, you can directly access the object from other Java programs.

If you want to access objects from the POJO class, you will have to follow the following steps:

  • Create a POJO class object.
  • Set the values using the set () method.
  • Get the values using the get () method.

The following is an example where a MainClass.java is created, and the objects of the POJO class are used for implementation

Sample Code

//Using POJO class objects in MainClass Java program
public class MainClass
{
public static void main (String args [] )
{
//Create an Employee class object
Employee obj = new Employee ();
obj.setName (“ Alisha ”); //Setting the values using the set () method
obj.setId(“ A001 ”);
obj.setSal ( 20000 );
System.out.println (“Name : ” + obj.getName () ); //Getting values using the get () method
System.out.println (“ ID : ” + obj.getID () );
System.out.println (“ Salary : ” + obj.getSal () );
}
}

 

OUTPUT

Name: Alisha
Id: A001
Salary: 20000

This above example clearly shows that we have created an object of the POJO class inside the MainClass and used the POJO class properties with the help of the setter and the getter methods.

Java Bean

Java Bean is also a class that is used to create objects. Here several objects are conglomerated into a single file called the Bean Class File. Although both the Bean and the POJO class look very similar with regards to their working from the outside, it is, however, not the case. There are subtle differences that are laid out below:

  • All the Bean files in Java can be plain old java objects, but all plain old java objects cannot be beans.
  • All Bean files can implement a serializable interface. However, a plain old java object cannot always implement all Serializable interfaces.
  • Both the properties should be private such that ultimate control over the fields resides with the developer.
  • Both the Bean and the Plain old java objects must have the getter and setter methods. The setter method sets values in the object, while the getter methods return values from the object. Without these two methods, it will not be possible for these objects to be accessed inside other Java classes.
  • The Bean class is the sub-set of the POJO class. This follows the first bullet point that says all Bean files can be POJOs, but all POJOs are not beans.
  • As POJOs have absolutely no restrictions, Beans still have a few restrictions that must be adhered to. Therefore, even if you face some disadvantages using the Bean class, you will not face any using the Plain old java object class.
  • The POJO is used specifically when we want to provide full access to users and restrict our members. The Bean is used when we want to provide partial access to the members.

Plain Old Java Object VS Beans

POJOBean
Other than the basic Java conventions that apply universally, there are no restrictions in POJO.Beans, as mentioned before, is a special type of POJO with some restrictions other than the universal Java conventions.
Users exercise less control over the fields as compared to beans.Beans provide complete protection on fields.
A plain old java object cannot always implement a Serializable interface.Beans can implement the Serializable interface at all times.
You can access a POJO class using the class name.You can only access a Bean class via the setter and the getter methods.
In POJO, the fields can have any of the following access modifiers: private, public, or protected.In Beans, fields can only have the private access modifier.
In POJO, it is not a mandate that you need to have the no-argument constructor. You may or may not have it.In Beans, your class must have a no-arguments constructor.
You will not face any disadvantage in using POJO as it is completely unrestricted.One of the major disadvantages of using Beans is that the Default constructor and the public setter method can change the object state when it should be immutable.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.