1. Overview

In this tutorial, we’ll look at the Lombok @Getter and @Setter annotations to generate getter and setter methods automatically.

2. Use @Getter and @Setter on Class

When we annotate a class with @Getter and @Setter, Lombok generates the getter and setter methods for all non-static fields.

We’ll work with the Account class:

@Getter
@Setter
public class Account {

    private String username;
    private String password;
}

It has two fields, username, and password. Also, we’re annotating the class with the @Getter and @Setter annotations.

When we compile our code, Lombok must generate getters and setters for username and password:

public class Account {

    private String username;
    private String password;

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Indeed, the getter and setter methods are created.

3. Use @Getter and @Setter on Fields

We can also annotate instance fields with @Getter and @Setter. Consequently, @Getter generates a getter method and @Setter generates a setter method for the annotated field:

public class Account {

    @Getter
    @Setter
    private String username;

    @Getter
    @Setter
    private String password;
}

Instead of placing the annotations onto the class, we’re placing them onto the fields. Nevertheless, the generated methods will be very similar to the previous example:

public class Account {

    private String username;
    private String password;

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4. Configure @Getter and @Setter with @Accessors

We can fine-tune the behavior of @Getter and @Setter by using the @Accessors annotation.

For example, we can make getters/setters fluent, so that getName becomes name.  We can also make setters chainable so that setName returns the current instance instead of void.

Let’s see the usage with the Account class:

@Getter
@Setter
@Accessors(fluent = true, chain = true)
public class Account {

    private String username;
    private String password;
}

Here we’re adding the @Accessors annotation to the class. Then we’re stating that getters and setters should be fluent and chainable.

When we look at the generated code:

public class Account {

    private String username;
    private String password;

    public String username() {
        return this.username;
    }

    public String password() {
        return this.password;
    }

    public Account username(String username) {
        this.username = username;
        return this;
    }

    public Account password(String password) {
        this.password = password;
        return this;
    }
}

We have the accessor methods, but get/set prefixes aren’t there anymore. Furthermore, setter methods return the current instance, namely this.

5. Summary

In this tutorial, we’ve looked at how we can use the Lombok @Getter and @Setter annotations to generate accessors methods.

As always, the source code is available on Github.

Leave a Reply

Close Menu