๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

POJO Classes & JavaBean Classes


POJO Classes


What are POJO Classes ?
  • POJO stands for Plain Old Java Object.
  • POJO is a simple Java class that doesn't adhere or follows to any specific conventions or interfaces.
  • POJO class typically consists of :
    1. Private fields : Variables should be private to maintain encapsulation.
    2. Public getter and setter methods : To access and modify the private fields.
  • For example :
    public class Student
    {
        private String name;
        private int rollno;
    
        // Constructor
        public Student(String name, int rollno)
        {
            this.name = name;
            this.rollno = rollno;
        }
    
        // Getter and setter methods
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getRollno()  
        {
            return rollno;
        }
    
        public void setRollno(int rollno)
        {
            this.rollno = rollno;  
    
        }
    }

Use of POJO Classes ?
  • POJO classes are mainly used for data representation and data transfer.
  • Below are some common uses of POJO classes :-
    1. Encapsulation : POJO classes encapsulate data in an object, making it easier to manage and access data using methods rather than exposing fields directly.
    2. Data Transfer : POJO classes are commonly used as Data Transfer Objects (DTOs), allowing the easy transfer of data between layers of an application (e.g., between a database and a frontend).
    3. Readability and Maintainability : POJO classes make the code cleaner and easier to maintain by clearly defining fields and providing accessor methods.
    4. Framework Compatibility : Most Java frameworks, such as Hibernate, Spring, and JPA, use POJO classes to map data from a database or an external source to an object.
    5. Testing : POJO classes can be used to create test data for unit and integration tests.

JavaBean Classes


What are JavaBean Classes ?
  • A JavaBean is a specialized type of POJO that follows specific conventions :-
    1. Private Fields : Variables should be private to maintain encapsulation.
    2. Public Getter and Setter Methods : To access and modify the private fields.
    3. No-Argument Constructor : Enables instantiation of the JavaBean without setting any initial values.
    4. Serializable Interface : JavaBeans often implement Serializable for compatibility with certain frameworks.
  • For example :
    public class Student implements Serializable
    {
        private String name;
        private int rollno;
    
        // No-argument constructor
        public Student() {}
    
        // Parameterized constructor (strictly not required but good practice)
        public Student(String name, int rollno)
        {
            this.name = name;
            this.rollno = rollno;
        }
    
        // Getter and setter methods
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getRollno()  
        {
            return rollno;
        }
    
        public void setRollno(int rollno)
        {
            this.rollno = rollno;  
    
        }
    }

Use of JavaBean Classes ?
  • POJO classes are mainly used for data representation and data transfer.
  • Below are some common uses of POJO classes :-
    1. Encapsulation : JavaBeans allow data encapsulation with strict conventions, providing standardized access and modification through getters and setters.
    2. Data Transfer : JavaBeans are frequently used as Data Transfer Objects (DTOs) for transferring data between application layers.
    3. Readability : JavaBeans are designed as reusable components in applications, promoting modularity and flexibility.
    4. Framework Compatibility : Frameworks like Spring and Hibernate rely on JavaBeans for object mapping, configuration, and data manipulation.
    5. Testing: JavaBean classes can be used to create test data for unit and integration tests.
    6. IDE Support and Interoperability : JavaBeans conventions allow easy integration with IDEs, simplifying tasks like visual component manipulation and automatic code generation.

Difference between POJO Classes & JavaBean Classes :-

Aspect POJO (Plain Old Java Object) JavaBean
Definition A simple Java class with no restrictions or specific conventions. A specialized POJO following specific conventions.
Requirements No strict requirements; can have any structure or methods. Must have private fields, public getter and setter methods, and a no-argument constructor.
Serialization Optional; may or may not implement Serializable. Often implements Serializable for compatibility with frameworks.
Constructor No restriction on constructors (can have or omit any constructor). Must include a no-argument constructor for instantiation by frameworks.
Framework Use Less dependent on frameworks, mainly for general-purpose objects. Commonly used in frameworks (e.g., Spring, Hibernate) for mapping data.
Use Case General object representation, data transfer, or encapsulation. Data encapsulation with specific structure, reusable components, and data transfer objects (DTOs).

Choosing between POJO and JavaBean :-

  • POJO Classes : POJO Classes are used when we need a simple, plain Java object without any specific framework requirements.
  • JavaBean Classes : JavaBean Classes are used when we need a more structured object that can be easily serialized, persisted and integrated with frameworks.