Signup/Sign In

Java POJO Class

POJO stands for Plain Old Java Objects. They are normal objects that have no restrictions. They are not bound to any framework. They are easy to create and increase the readability and reusability of our code. In this tutorial, we will learn more about POJO.

Plain Old Java Objects

As discussed above, these objects do not have any restrictions on them. They can be created and used in any way. The methods and properties of POJO do not need to follow any naming conventions.

An example of POJO is shown in the Sample class. It contains three properties. These properties do not follow any particular access rule. Field1 is public, whereas field2 is private. No fixed convention is used to define the setters and getters. Field1 and Field3 have getters, but no setters are used in the class.

//POJO
public class Sample
{
	public String field1;
	private int field2;
	double field3;
	
	Sample(String s, int i, double d)
	{
		this.field1 = s;
		this.field2 = i;
		this.field3 = d;
	}	
	public String getField1()
	{
		return this.field1;
	}	
	public double getField3()
	{
		return this.field3;
	}
}

There are a few things that should not be a part of any POJO class.

  • A POJO class should not extend any class.
  • It should not implement any interface.
  • A POJO should not contain any prespecified annotations.

Due to a lack of conventions, POJO classes may be difficult to understand by other programmers. POJO classes may not work perfectly fine when analyzed using Reflection API. For example, if we try to view the properties of the POJO class defined earlier, then we will not get the expected results. The data for field2 is missing.

public static void main(String[] args) 
{
	Sample s = new Sample("f1", 0, 0.0);
	try
	{
		Map<String, Object> properties = BeanUtils.describe(s);//Fetching object class details
		for(Map.Entry<String, Object> e : properties.entrySet())
			System.out.println(e.getKey() + "->" + e.getValue());
	}
	catch(Exception e)
	{
		System.out.print(e);
	}		
}


field1->f1
field3->0.0
class->class Sample

Java Beans

Java Bean classes are a special type of POJO classes. All Java Bean classes are POJOs, but not all POJOs can be called Java Beans. To rectify some problems of POJO classes, Java Bean classes must follow some standard rules and conventions. The conventions that a Java Bean class should follow are given below.

  • All class properties must be private.
  • Standard Getter and Setter methods must be written.
  • A default no-argument constructor needs to be created.
  • This class must implement the Serializable interface.

Let's convert the Sample POJO class to a Java Bean class.

import java.io.Serializable;
//JavaBean
public class Sample implements Serializable//Class must implement Serializable
{
	//All fields must be private
	private String field1;
	private int field2;
	private double field3;
	
	//A default no-argument constructor must be present
	Sample()
	{}
	
	Sample(String s, int i, double d)
	{
		this.field1 = s;
		this.field2 = i;
		this.field3 = d;
	}
	//Getters and Setters must be present for all fields
	public String getField1() {
		return field1;
	}
	public void setField1(String field1) {
		this.field1 = field1;
	}
	public int getField2() {
		return field2;
	}
	public void setField2(int field2) {
		this.field2 = field2;
	}
	public double getField3() {
		return field3;
	}
	public void setField3(double field3) {
		this.field3 = field3;
	}
}

Now, we can also use reflection to view the properties. All three fields are displayed in the output.

public static void main(String[] args) 
{
	Sample s = new Sample("f1", 0, 0.0);
	try
	{
		Map<String, Object> properties = BeanUtils.describe(s);
		for(Map.Entry<String, Object> e : properties.entrySet())
			System.out.println(e.getKey() + "->" + e.getValue());
	}
	catch(Exception e)
	{
		System.out.print(e);
	}		
}


field1->f1
field3->0.0
class->class Sample
field2->0

Java Bean classes solve some of the issues of the POJO classes, but they are not perfect. Our Java Bean classes must have a getter and setter method for every property, even when there is no requirement for it. Java Bean class properties are mutable because of the presence of mandatory setter methods. The default constructor is not needed in some cases, but it is mandatory for a Java Bean class.

Summary

POJO or plain old Java objects are simple objects that do not need to follow any strict convention. A user can create a POJO class in any way. Java Bean classes try to solve the issues of POJO classes by imposing a set of rules on them.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.