JavaBean
A JavaBean is a Java class that follows the JavaBeans specification: a public no-argument constructor, private fields, public getter/setter methods following a naming pattern, and (traditionally) implementation of Serializable. Reflection-based frameworks β Spring, JPA, Jackson, older GUI builders β rely on these conventions.
Example
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person() {} // required no-arg constructor
public String getName() { return name; }
public void setName(String n){ this.name = n; }
public int getAge() { return age; }
public void setAge(int a) { this.age = a; }
}
Property naming convention
- For a property
age: getter isgetAge(), setter issetAge(int). - For a
booleanpropertyactive: getter can beisActive()instead ofgetActive(). - Naming is strict β frameworks discover properties by matching these patterns via reflection.
JavaBean vs POJO vs record
- POJO β any simple Java class with no special constraints.
- JavaBean β a POJO that also follows the JavaBeans naming convention.
- Record β the modern replacement for immutable data carriers (Java 16+). Records are not strictly JavaBeans (accessors are
age(), notgetAge()) but most frameworks now support both patterns.