×

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

  1. Create Pojo in a program
  2. By using the set method(), set values
  3. 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

POJO in Java

Related Topics

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.