Lombok: Never write another getters/setters or equals method

Table of contents
Reading Time: 2 minutes

Being shifted from scala to java in one of my projects, I was not happy as I would have to write 10 lines of code in Java for a single line of Scala code. Thankfully, I was introduced to Lombok through one of our clients.

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Let me first give you a simple example of normal java v/s scala code.

For a POJO Employee in java,

public class Employee {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

There is just a single line of Scala code –

case class Employee(id: String, name: String)

You can see how much less code needs to be written in scala as compared to java.

Though Java is now trying to come closer to scala by moving more towards functional style. But still, Java is not Scala and Java developer needs to write getters/setters for their POJO’s, unlike scala way of code.

In this blog, I am going to introduce you to Lombok which allows java developer not to write any more getters/setters by giving Lombok annotations for them.

Let me explain this with an example –

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    private String id;
    private String name;
}

This way we do not have to write any getters, setters, or constructors.

By providing such annotations, Lombok has helped reduce java lines of code. Let us now look into different annotations provided by Lombok.

Most of these annotations are self-explanatory by their names. Few examples are – @ToString, @EqualsAndHashCode, @Getter, @Setter, @NonNull

@Data


@Data annotation is a shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor

So we can use this like below –

@Data
public class Employee {
 private String id;
 private String name;
}

@Value


@Value annotation is the immutable variant of @Data; all fields are made private and final by default, and setters are not generated. The class itself is also made final by default because immutability is not something that can be forced onto a subclass.

@Value
public class ValueExample {
    String name;
    String id;
}

@Builder


@Builder produces complex builder APIs for your classes.

The advantage of using this annotation is explained using following example –

@Data
@Builder
public class Employee {
    private String id;
    private String name;
}

When instantiating this class, instead of writing

Employee emp = new Employee(“E11”, “xyz”)

We can now do –

Employee emp = Employee.builder()
        .id("E11")
        .name("abc")
        .build();

@val


@val is the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final.

Limitation for @val: This feature works on local variables and on foreach loops only, not on fields.

String extractFirstInLower(ArrayList<String> list) {
    list.add("Hello, World!");
    val foo = list.get(0);
    return foo.toLowerCase();
}

@CleanUp


Automatic resource management: Call your close() methods safely with no hassle.

Properties properties = new Properties();
@Cleanup
InputStream in = new FileInputStream("application.conf");
properties.load(in);
String key = properties.getProperty("private_key");
log.info("key: " + key);

Though, I recommend using try block with resources instead of @CleanUp.

There are many more annotations like @var, @NonNull, @Log, @Synchronized etc..

For complete code, please refer to my GitHub repository.

Keep exploring !!

 

 

Written by 

Simar is a Software Consultant, having experience of more than 1.5 years. She has an interest in both object-oriented and functional programming. She is a java enthusiast and is now indulged in learning Scala programming language. She is also familiar with relational database technologies such as MySQL, and NoSql database technologies as well. She has worked on Kafka, Spark, Hadoop etc. Her hobbies include coding and listening to music.

1 thought on “Lombok: Never write another getters/setters or equals method3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading