POJO in Java
Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java program. There is no unique class path needed for the POJO file. It improves a Java program's ability to be read and reused. It is simple for everyone to read and write them. There is no naming scheme for properties and methods in a POJO class. It is independent of the Java Framework.
As both describe the objects to promote readability and reusability, POJO classes are comparable to Beans in this regard. The sole distinction between them is that whereas POJO files do not have any unique constraints, Bean Files do.
Syntax
Syntax.java
public class Syntax
{
String name1;
public String id1;
private double sal1;
general Syntax(String name, String id,double salary)
{
this.name1 = name;
this.id1 = id;
this.salary1 = salary;
}
public String getName()
{
return name1;
}
public String getId()
{
return id1;
}
public Double getSalary()
{
return sal1;
}
}
Select Generate Getters and Setters from the context menu when you right-click the Java programme.
Pojo Properties
- To allow other Java programmes to access an object's values, all things must have some public Getters and Setters.
- The POJO Class object can have any access modifiers, including private, public, and protected. But for increased project security, all instance variables should be private.
- Predefined classes shouldn't be extended by POJO classes.
- Pojo should not use predetermined interfaces.
- There shouldn't be any predetermined annotations on it.
- The public POJO class is required.
- A public default constructor is compulsory in Pojo.
Steps to use Pojo in a program
- Create Pojo in a program
- By using the set method(), set values
- Obtain the values using the get method()
Program for Pojo
PojoExample.java
// Creating a main public class which contains the primary main method, which is the starting point for execution
import java . util . * ;
import java . io . * ;
public class PojoExample {
// Main section of the code where execution starts
public static void main ( String [ ] args )
{
// Creating an Object for Employee class
Person o1 = new Person ( ) ;
// Setting the name value by using the set() method
Scanner sc = new Scanner ( System . in ) ;
System . out . println ( " Enter the name of the person " ) ;
String s1 = sc . next ( ) ;
o1 . setName ( s1 ) ;
// Setting the id value by using the set() method
System . out . println ( " Enter the id of the person " ) ;
String i = sc . next ( ) ;
o1 . setId ( i ) ;
// Setting the salary value by using the set() method
System . out . println ( " Enter the salary of the person " ) ;
int sal = sc . nextInt ( ) ;
o1 . setSal ( sal ) ;
//Getting the value of the name of the person using the get() method
System . out . println ( " Name of the person is : " + o1 . getName ( ) ) ;
//Getting the value of the id of the person using the get ( ) method
System . out . println ( " Id of the person is : " + o1 . getId ( ) ) ;
//Getting the value of salary of the person using the get ( ) method
System . out . println ( " Salary of the person is : " + o1 . getSal ( ) ) ;
}
}
class Person {
private String s ;
private String id1 ;
private int sal1 ;
public String getName ( ) {
return s ;
}
public void setName ( String name1 ) {
this . s = name1 ;
}
public String getId ( ) {
return id1 ;
}
public void setId ( String id ) {
this . id1 = id ;
}
public int getSal( ) {
return sal1 ;
}
public void setSal ( int salary ) {
this . sal1 = salary ;
}
}
Output
