POJO stands for Plain Old Java Object and it is an ordinary Java objects not a special kind of. The term POJO was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000 when they were talking on many benefits of encoding business logic into regular java objects rather than using Entity Beans or JavaBeans.
From the full form of OOPs , we can understand that it is a programming approach which is oriented around something called “Object” and an Object can contain data (Member variables ) and mechanism (member functions) to manipulate on these data.
A POJO class can follow some rules for better usability. These rules are :-
- Each variable should be declared as private just to restrict direct access.
- Each variable which needs to be accessed outside class may have a getter or a setter or both methods. If value of a field is stored after some calculations then we must not have any setter method for that.
- It Should have a default public constructor.
- Can override toString(), hashcode and equals() methods.
- Can contain business logic as required.
There are some restrictions imposed by Java language specifications on POJO. A POJO should not :-
- Extend prespecified classes
- Implement prespecified interfaces
- Contain prespecified annotations
Advantages of POJO :-
- Increases readability
- Provides type checks
- Can be serialized and deserialized
- Can be used anywhere with any framework
- Data Manipulation is easier. We can have logic before setting and getting a value.
- Builder pattern can be used with POJO.
- Frequently used to create payloads for API.
- Reusability