@Getter & @Setter
@Getter and @Setter are the most fundamental Lombok annotations. They generate standard JavaBean accessor methods for your fields so you never have to write them by hand. You can apply them to a single field or to an entire class, and you can control the visibility of the generated methods.
Class-level accessors
Placing @Getter and @Setter on the class generates a getter and setter for every non-static field.
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
private Long id;
private String name;
private double price;
}
This is equivalent to writing:
public class Product {
private Long id;
private String name;
private double price;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
Lombok follows JavaBean naming: boolean fields get an is-prefixed getter (isActive() for a field named active).
Field-level accessors
Annotate individual fields when you want accessors for only some of them — for example an immutable id with a getter but no setter:
@Getter
public class Order {
private final Long id; // getter only
@Setter private String status; // getter + setter
private Instant createdAt; // getter only
}
Field-level annotations override or supplement class-level ones, giving you precise control.
Controlling access levels
Both annotations accept an AccessLevel to change the visibility of the generated method. This is useful for fields that should be read or written only within the package or by subclasses.
import lombok.AccessLevel;
public class Account {
@Getter
@Setter(AccessLevel.PROTECTED)
private BigDecimal balance;
@Getter(AccessLevel.NONE)
private String internalToken; // no getter generated at all
}
AccessLevel | Generated method visibility |
|---|---|
PUBLIC (default) | public |
PROTECTED | protected |
PACKAGE | package-private |
PRIVATE | private |
NONE | suppresses generation |
Use AccessLevel.NONE to exclude one field when the rest of the class is annotated at class level.
Lazy getters
A @Getter(lazy = true) computes an expensive value once on first access and caches it thread-safely. The field must be private final and initialized with the costly expression.
import lombok.Getter;
public class ConfigCache {
@Getter(lazy = true)
private final Map<String, String> settings = expensiveLoad();
private static Map<String, String> expensiveLoad() {
// heavy I/O or computation
return Map.of("region", "us-east-1");
}
}
Lombok generates a double-checked-locking getter that runs expensiveLoad() only the first time getSettings() is called and returns the cached result thereafter.
// roughly what Lombok generates
private final AtomicReference<Object> settings = new AtomicReference<>();
public Map<String, String> getSettings() {
Object value = this.settings.get();
if (value == null) {
synchronized (this.settings) {
value = this.settings.get();
if (value == null) {
Map<String, String> actual = expensiveLoad();
value = (actual == null ? this.settings : actual);
this.settings.set(value);
}
}
}
return (Map<String, String>) (value == this.settings ? null : value);
}
Tip: Reach for a lazy getter only when the initialization is genuinely costly. For ordinary fields a plain getter is clearer and faster.
Interaction with Spring Boot
In a Spring application @Getter/@Setter are most common on configuration property classes and DTOs, where the framework needs accessors to bind or serialize values.
@ConfigurationProperties(prefix = "app.mail")
@Getter
@Setter
public class MailProperties {
private String host;
private int port;
private boolean tlsEnabled;
}
Jackson and Spring’s binder use the generated setters to populate the object, so the boilerplate disappears while the behavior stays identical. For richer bundles that add toString/equals, see @Data.
Warning: Generating setters for every field makes a class mutable. For values that should never change after construction, prefer
@Getteronly — or a record DTO.