POJO Class in Java (Plain Old Java Object with Example)

Free Java courses with 37 real-time projects - Learn Java

1. Objective

In the last tutorial, we discussed Final Keyword in Java. Here, in this Java POJO tutorial, we are going to study the POJO classes in Java. Moreover, we will discuss the Why should we use POJO Class in Java with examples.

So, let us start POJO Class in Java.

POJO Class in Java (Plain Old Java Object with Example)

POJO Class in Java (Plain Old Java Object with Example)

2. What is POJO Class in Java?

POJO stands for Plain Old Java Object. A normal Java object, not sure by any special restriction. But when forced by the Java Language Specification and not requiring any classpath. POJOs area unit used for increasing the readability and reusability of a program. They’re simple to put in writing and perceive. They were introduced in EJB three.0 by Sun Microsystems.

A POJO shouldn’t contain the following:

Extend pre-specified classes, ex: public class GFG extends javax.servlet.http.HttpServlet isn’t a POJO class.

Contain pre-specified annotations, ex: @javax.persistence.Entity public class Baz isn’t a POJO class.

Read About Java Character Class Methods

3. POJO Class in Java Example

POJOs basically defines an entity. Like in your program, if you want an employee class then you can create a POJO as follows:

public class Employee
{
String name;
public String id;
private double salary;
public Employee(String name, String id,
double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName()
{
return name;
}
public String getId()
{
return id;
}
public Double getSalary()
{
return salary;
}
}

The above example is a well-defined example of POJO class. As you can see, there is no restriction on access-modifier of fields. They can be private, default, protected or the public. It is also not necessary to include any constructor in it.

To receive messages from JMS, you wish to put in writing a class that implements the MessageListener interface.

public class ExampleListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println(((TextMessage) message).getText());
}
catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
public class ExampleListener implements MessageListener
public void
}

In this example, your code is not directly tied to any interface. Instead, the responsibility of connecting it to a JMS queue affect by annotations, that square measure easier to update. during this specific example, you’ll replace @JmsListener with @RabbitListener. In alternative things, it’s potential to possess POJO-based solutions with none specific annotations.

Do you know what is Serialization and Deserialization in Java

@Component
public class ExampleListener {
@JmsListener(destination = "myDestination")
public void processOrder(String message) {
System.out.println(message);
}
}

This is only one example. It’s not meant as an example JMS vs. RabbitMQ, however instead of the worth of writing coding being tied to specific interfaces. By using plain old Java objects, your code is often a lot less complicated. This lends itself to better testing, flexibility, and talent to create new decisions within the future.

So, this was all about POJO class in Java. Hope you like our explanation.

4. Conclusion

Hence, in this tutorial, we learned about the Introduction to POJO class in Java. In addition, we discuss an example of a Java POJO Class. Furthermore, if you have any Query, feel free to ask in the comment section.

Related Topic-  Java Virtual Machine

For reference

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. Hrituparn Alreja says:

    After point no. 3 it seems complicated. I understood till 3 but not after that.Please make it more simplified. What I cant understand that, How to use this POJO file in main code?

Leave a Reply

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