×

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

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.