BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

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

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.

What happens, in order
  1. 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.
  2. 2BeanUtils.copyProperties(source, target) (Spring or Commons) copies properties with matching names; Introspector.getBeanInfo(X.class).getPropertyDescriptors() lists them using reflection
  3. 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
Remember
Watch out
A getter that does heavy work or has side effectsRelying on copyProperties for deep copiesPublic mutable fields instead of accessors
Example (highlighted lines explain it)
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());  }}
  1. 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 copyProperties for 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:

Part of a learning path

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.