Unravel โบ Java concepts โบ OOP and types
JavaBeans, POJOs and BeanUtils
A JavaBean (or POJO) is a plain class with a no-argument constructor, private fields and public getX/setX methods.
Learn these first
At a glance
A standard form with labelled boxes: any clerk (framework) can fill in or read the "name" box by its label without knowing anything else about the form.
- 1Conventions: private String name; public String getName(); public void setName(String n); and isActive() for booleansProperty name is derived from the method name, not from the field.
- 2BeanUtils.copyProperties(source, target) (Spring or Commons) copies properties with matching names; Introspector.getBeanInfo(X.class).getPropertyDescriptors() lists them using reflection
- 3Data holders (DTOs, entities, configuration properties) are usually beans; with Java 16+ a record is the better choice for immutable data, and Lombok @Data removes the boilerplate
- copyProperties is convenient but unchecked: a renamed field silently stops being copied, null values overwrite, and it copies only shallowly
- Setters make objects mutable and allow half-initialised states
- The Introspector inspects the class with reflection, caches a BeanInfo, and exposes properties as PropertyDescriptor pairs of read and write Method objects, which frameworks invoke by name
import java.beans.*;public class Person { private String name; private boolean active; public Person() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isActive() { return active; } public void setActive(boolean active) { this.active = active; } static void list() throws IntrospectionException { for (PropertyDescriptor pd : Introspector.getBeanInfo(Person.class).getPropertyDescriptors()) System.out.println(pd.getName()); }}- line 10 uses PropertyDescriptor, the core of JavaBeans, POJOs and BeanUtils
Read the full explanation (about 1 min)
A JavaBean (or POJO) is a plain class with a no-argument constructor, private fields and public getX/setX methods. Frameworks (JSON mappers, ORMs, template engines, BeanUtils) rely on those conventions to read and write properties by name.
A mental model
A standard form with labelled boxes: any clerk (framework) can fill in or read the "name" box by its label without knowing anything else about the form.
How it works
Conventions: private String name; public String getName(); public void setName(String n); and isActive() for booleans. Property name is derived from the method name, not from the field.
BeanUtils.copyProperties(source, target) (Spring or Commons) copies properties with matching names; Introspector.getBeanInfo(X.class).getPropertyDescriptors() lists them using reflection.
Data holders (DTOs, entities, configuration properties) are usually beans; with Java 16+ a record is the better choice for immutable data, and Lombok @Data removes the boilerplate.
Going deeper
copyProperties is convenient but unchecked: a renamed field silently stops being copied, null values overwrite, and it copies only shallowly. For important mappings prefer MapStruct or explicit code, which the compiler can check.
Setters make objects mutable and allow half-initialised states. Prefer constructor injection and immutable objects when the framework allows it.
Frameworks that need a no-arg constructor can use a protected or private one (JPA, Jackson with annotations).
Under the hood
The Introspector inspects the class with reflection, caches a BeanInfo, and exposes properties as PropertyDescriptor pairs of read and write Method objects, which frameworks invoke by name.
Common mistakes
- A getter that does heavy work or has side effects.
- Relying on
copyPropertiesfor deep copies. - Public mutable fields instead of accessors.
How Unravel recognises it
When you paste code, Unravel looks for these signs of javabeans, pojos and beanutils. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
BeanUtilsIntrospectorPropertyDescriptorBeanInfoPropertyEditorBeanWrapperPropertyUtils - Method calls:
copyProperties()getPropertyDescriptors()
Part of a learning path
- JDBC and everyday libraries ยท previous: MapStruct and object mapping
Related concepts
Frequently asked questions
What is JavaBeans, POJOs and BeanUtils?
A JavaBean (or POJO) is a plain class with a no-argument constructor, private fields and public getX/setX methods. Frameworks (JSON mappers, ORMs, template engines, BeanUtils) rely on those conventions to read and write properties by name.
What are the most common mistakes with JavaBeans, POJOs and BeanUtils?
A getter that does heavy work or has side effects. Relying on copyProperties for deep copies. Public mutable fields instead of accessors.
How can I see JavaBeans, POJOs and BeanUtils in real code?
Paste your own Java into Unravel and it will point out where javabeans, pojos and beanutils appears, explain how it works at the detail level you choose, and draw the flow of the code around it.