eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Overview

It’s pretty typical to have get and set methods in our domain objects, but there are other ways that we may find more expressive.

In this tutorial, we’ll learn about Project Lombok‘s @Accessors annotation and its support for fluent, chained, and custom accessors.

Before continuing, though, our IDE will need Lombok installed.

2. Standard Accessors

Before we look at the @Accessors annotation, let’s review how Lombok treats the @Getter and @Setter annotations by default.

First, let’s create our class:

@Getter
@Setter
public class StandardAccount {
    private String name;
    private BigDecimal balance;
}

And now let’s create a test case. We can see in our test that Lombok has added typical getter and setter methods:

@Test
public void whenStandardAccount_thenHaveStandardAccessors() {
    StandardAccount account = new StandardAccount();
    account.setName("Standard Accessors");
    account.setBalance(BigDecimal.TEN);

    assertEquals("Standard Accessors", account.getName());
    assertEquals(BigDecimal.TEN, account.getBalance()); 
}

We’ll see how this test case changes as we look at @Accessor‘s options.

3. Fluent Accessors

Let’s begin with the fluent option:

@Accessors(fluent = true)

The fluent option gives us accessors that don’t have a get or set prefix.

We’ll take a look at the chain option in a moment, but since it’s enabled by default, let’s disable it explicitly for now:

@Accessors(fluent = true, chain = false)
@Getter
@Setter
public class FluentAccount {
    private String name;
    private BigDecimal balance;
}

Now, our test still behaves the same, but we’ve changed the way we access and mutate state:

@Test
public void whenFluentAccount_thenHaveFluentAccessors() {
    FluentAccount account = new FluentAccount();
    account.name("Fluent Account");
    account.balance(BigDecimal.TEN);

    assertEquals("Fluent Account", account.name()); 
    assertEquals(BigDecimal.TEN, account.balance());
}

Notice how the get and set prefixes have disappeared.

4. Chained Accessors

Now let’s take a look at the chain option:

@Accessors(chain = true)

The chain option gives us setters that return thisAgain note that it defaults to true, but we’ll set it explicitly for clarity.

This means we can chain multiple set operations together in one statement.

Let’s build on our fluent accessors and change the chain option to true:

@Accessors(fluent = true, chain = true)
@Getter 
@Setter 
public class ChainedFluentAccount { 
    private String name; 
    private BigDecimal balance;
}

We get the same effect if we leave out chain and just specify:

@Accessors(fluent = true)

And now let’s see how this affects our test case:

@Test
public void whenChainedFluentAccount_thenHaveChainedFluentAccessors() {
    ChainedFluentAccount account = new ChainedFluentAccount()
      .name("Fluent Account")
      .balance(BigDecimal.TEN);

    assertEquals("Fluent Account", account.name()); 
    assertEquals(BigDecimal.TEN, account.balance());
}

Notice how the new statement becomes longer with the setters chained together, removing some boilerplate.

This, of course, is how Lombok’s @Builder makes use of chained fluent accessors.

5. Prefix Accessors

At times our fields may have a different naming convention than we’d like to expose via getters and setters.

Let’s consider the following class that uses Hungarian Notation for its fields:

public class PrefixedAccount { 
    private String sName; 
    private BigDecimal bdBalance; 
}

If we were to expose this with @Getter and @Setter, we’d get methods like getSName, which isn’t quite as readable.

The prefix option allows us to tell Lombok which prefixes to ignore:

@Accessors(prefix = {"s", "bd"})
@Getter
@Setter
public class PrefixedAccount {
    private String sName;
    private BigDecimal bdBalance;
}

So, let’s see how that affects our test case:

@Test
public void whenPrefixedAccount_thenRemovePrefixFromAccessors() {
    PrefixedAccount account = new PrefixedAccount();
    account.setName("Prefixed Fields");
    account.setBalance(BigDecimal.TEN);

    assertEquals("Prefixed Fields", account.getName()); 
    assertEquals(BigDecimal.TEN, account.getBalance());
}

Notice how the accessors for our sName field (setName, getName) omit the leading s and the accessors for bdBalance omit the leading bd.

However, Lombok only applies prefixes when a prefix is followed by something other than a lowercase letter.

This makes sure that if we have a field that isn’t using Hungarian Notation, such as state, but starts with one of our prefixes, s, we don’t end up with getTate()!

Lastly, let’s say we want to use underscores in our notation but also want to follow it with a lowercase letter.

Let’s add a field s_notes with prefix s_:

@Accessors(prefix = "s_")
private String s_notes;

Following the lowercase letter rule we’d get methods like getS_Notes(), so Lombok also applies prefixes when a prefix itself ends in something that isn’t a letter.

6. Final Accessors

Finally, since version v1.18.24, Lombok supports the makeFinal option to make the generated getters and setters final:

@Accessors(makeFinal = true)

This is pretty helpful when our class has subclasses, but they aren’t allowed to override our getters and setters.

So next, let’s create a FinalAccount class with this option:

@Accessors(makeFinal = true)
@Getter
@Setter
public class FinalAccount {
    private String name;
    private BigDecimal balance;
}

Now let’s write a test to verify if the getters and setters work as usual and whether they are final methods:

@Test
public void whenFinalAccount_thenHaveFinalAccessors() {
    FinalAccount account = new FinalAccount();
    account.setName("Final Account");
    account.setBalance(BigDecimal.TEN);

    assertEquals("Final Account", account.getName());
    assertEquals(BigDecimal.TEN, account.getBalance());

    //verify if all getters and setters are final methods
    boolean getterSettersAreFinal = Arrays.stream(FinalAccount.class.getMethods())
      .filter(method -> method.getName().matches("^(get|set)(Name|Balance)$"))
      .allMatch(method -> Modifier.isFinal(method.getModifiers()));
    assertTrue(getterSettersAreFinal);

}

In the test above, we first proved whether the getters and setters work as expected. Then, we used the Java Reflection API to verify if the getters and setters are final methods.

Of course, the makeFinal option can work together with other @Accessors options. Let’s see another example:

@Accessors(makeFinal = true, fluent = true, chain = true)
@Getter
@Setter
public class FinalChainedFluentAccount {
    private String name;
    private BigDecimal balance;
}

As the FinalChainedFluentAccount class shows, this time, we combine the makeFinal option together with fluent and chain. And let’s see how this affects our test case:

@Test
public void whenFinalChainedFluentAccount_thenHaveFinalAccessors() {
    FinalChainedFluentAccount account = new FinalChainedFluentAccount();
    account
      .name("Final Chained Fluent Account")
      .balance(BigDecimal.TEN);

    assertEquals("Final Chained Fluent Account", account.name());
    assertEquals(BigDecimal.TEN, account.balance());

    //verify if all getters and setters are final methods
    boolean getterSettersAreFinal = Arrays.stream(FinalAccount.class.getMethods())
      .filter(method -> method.getName().matches("^(name|balance)$"))
      .allMatch(method -> Modifier.isFinal(method.getModifiers()));
    assertTrue(getterSettersAreFinal);
}

As we can see in the test, the fluent methods name() and balance() are final.

7. Configuration Properties

We can set a project- or directory-wide default for our favorite combination of settings by adding configuration properties to a lombok.config file:

lombok.accessors.chain=true
lombok.accessors.fluent=true

See the Lombok Feature Configuration Guide for further details.

8. Conclusion

In this article, we used the fluent, chain, and prefix options of Lombok’s @Accessors annotation in various combinations to see how it affected the generated code.

To learn more, be sure to take a look at the Lombok Accessors JavaDoc and Experimental Feature Guide.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)