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;
}
}
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;
}
}
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). |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.