Trusted answers to developer questions

What is the @Setter annotation in Lombok?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

What is Lombok?

Project Lombok is a Java library that helps reduce boilerplate code. Java is a verbose language where repetitive code like getters, setters, etc., can be avoided. Lombok reduces the boilerplate code with its annotations that get plugged during the build process.

Lombok can easily be added to the project by adding it as one of the dependencies.

If the project is a Gradle project, we add the following two lines to the dependencies section of the build.gradle file:

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

If the project is a Maven project, we add the following two lines to the dependencies section of the pom.xml file:

<dependencies>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.22</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

Writing setters in vanilla Java

Consider we have a Person class with two attributes, age and name. The Person class has to provide setters for clients to set the attributes. The following code shows how to write setters in vanilla Java:

public class Main {
static class Person{
private int age;
private String name;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Person person = new Person();
person.setAge(15);
person.setName("sam");
System.out.println("Person Age - " + person.getAge());
System.out.println("Person Name - " + person.getName());
}
}

@Setter annotation

The @Setter annotation generates the default setter implementation for fields that are annotated with the annotation. This annotation can also be used at the class level in which Lombok generates the setter methods for all fields. The default implementation is to take a parameter of the same type as the annotated field and set the field to the received value. For example, if the field is int age, Lombok generates the setter method, setAge(int age). The generated method is public by default unless an AccessLevel is specified. The list of available access levels is PUBLIC, PROTECTED, PACKAGE, and PRIVATE.

The following code shows how the annotation reduces the generation of setters to a single annotation:

import lombok.Setter;

public class Main {

    static class Person{
        @Setter private int age;
        @Setter private String name;

        public int getAge() {
            return age;
        }

        public String getName() {
            return name;
        }
    }

    @Setter
    static class NewPerson{
        private int age;
        private String name;

        public int getAge() {
            return age;
        }

        public String getName() {
            return name;
        }
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setAge(15);
        person.setName("sam");
        System.out.println("Person Age - " + person.getAge());
        System.out.println("Person Name - " + person.getName());
        System.out.println("------");
        NewPerson newPerson = new NewPerson();
        newPerson.setAge(20);
        newPerson.setName("new-sam");
        System.out.println("New Person Age - " + newPerson.getAge());
        System.out.println("New Person Name - " + newPerson.getName());
    }
}
Setter annotation

RELATED TAGS

java
lombok
Did you find this helpful?