Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Everything You Need To Know About POJO In Java

Last updated on Jun 17,2021 10.4K Views


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 :

  • Extend pre-specified classes: Ex- public class Test extends javax.servlet.http.HttpServlet is not considered to be a POJO class.
  • Contain pre-specified annotations: Ex- @javax.persistence.Entity public class Test{..} is not a pojo class.
  • Implement prespecified interfaces: Ex- public class Test implements javax.ejb.EntityBean { … } is not considered to be a POJO class.

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.

POJO _ POJO In Java - EdurekaLet us understand Java Beans now,

Java Beans

Special types of Pojos are known as JavaBeans.

  • All JavaBeans can be considered as Pojos but not vice-versa.
  • The Serializable interface should be implemented by them.
  • Fields should be set to private, in order to provide the entire control on fields.
  • Fields must have getters, setters, or even both.
  • A bean must contain a no-arg constructor.
  • Fields can be accessed only by the constructors, or getters and setter.

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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 22nd February,2025

22nd February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need To Know About POJO In Java

edureka.co