Everything You Need To Know About POJO In Java

Last updated on Jun 17,2021 10.1K Views

Everything You Need To Know About POJO In Java

edureka.co

Pojo in Java stands for Plain Old Java Object and they are used for increasing the readability and re-usability of a program. They are normal java objects, unbounded by special restrictions, other than the ones forced by the Java Language Specification. This article will help you explore the subject in detail,

Following pointers will be covered in this article

So let us get started with this article on POJO in Java,

POJO In Java

In simpler terms, Pojo is defined as a pure data structure, containing the getter and setter fields.  It has the ability to override certain methods from Object or an interface such as Serializable. They were introduced in EJB 3.0 by Sun Microsystems and are widely used since they are easy to write and understand.

A POJO must not :

Moving on with this article on POJO in Java,

Example:

The given example is a clear-cut example of the POJO class. 

(// Student POJO class to represent entity Student
public class Student
{
// default field
String name;
//public field
public String id;
//private fees
private double fees;
//argument-constructor to initialize fields
public Student(String name, String id,
double fees)
{
this.name = name;
this.id = id;
this.fees = fees;
}
//getter method for name
public String getName()
{
return name;
}
//getter method for id
public String getId()
{
return id;
}
//getter method for Fees
public Double getFees()
{
return fees;
}
}

It can be seen that access-modifiers of fields do not contain any restriction, i.e. they are allowed to be default, protected, private or public.

Moving on with this article on POJO in Java,

Working of POJO

The working of the POJO class can be seen below. The business logic given in the picture is encapsulated by POJO. The controllers interact with the business logic , which in turn interacts with POJO for accessing the data base.

Let us understand Java Beans now,

Java Beans

Special types of Pojos are known as JavaBeans.

Moving on with this article on POJO in Java,

Getter and Setter

The getter and setter can be defined as follows:

Getter

public void getName()
{
return Name;
}

Setter

public void setName(name)
{
this.name=name;
}

Example

class Student implements java.io.Serializable{
private int id;
private String name;
public Student(){}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
public class Test{
public static void main(String args[]){
Student s=new Student(); //object is created
s.setName("Jeremy"); //setting value to the object
System.out.println(s.getName());
}
}

Output

Jeremy

The pojos in java are useful in defining objects to increase their readability and reusability. No other restrictions are imposed on them.

Beans, on the other hand, are defined as Pojos with certain restrictions

Thus we have come to an end of this article on ‘POJO in Java’. If you wish to learn more, check out the Java Online Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial