Will Lombok survive after Java Records?

Luiz Gustavo De O. Costa
3 min readNov 19, 2020

--

Why this question comes when Java launches records 💿?

Firstly let’s discuss what are Records, to understand each part of the discussion

[1] Records are a new kind of type declaration in the Java language. Like an enum, a record is a restricted form of class. It declares its representation, and commits to an API that matches that representation. Records give up a freedom that classes usually enjoy: the ability to decouple API from representation. In return, records gain a significant degree of concision.

A record has a name and a state description. The state description declares the components of the record. Optionally, a record has a body. For example:

record Point(int x, int y) { }

That’s so good in terms of lines because you don’t need anyone to create getters and setters just to support handle the object’s attributes.

On the other hand, using Lombok you have more than annotations such as @GettersAndSetters, you take advantage when you need to use a Builder pattern, instantiate final variables, and much more.

Let’s see the use of Lombok and Records and after you can consider what best for you in your project.

Lombok

To get starting with Lombok, you need to configure your IDE to accept Annotation Processing, in my case, using IntelliJ I have the following configuration.

Enable annotation processing on IntelliJ
Annotation Processing for project
Lombok plugin

Code — Lombok

Step by step.

1 — Add dependencies on pom.xml if you’re using Maven. The Lombok is for the API and the logback-classic is to use the annotation @Slf4j soon.

2 — Use the annotation @Data or the annotations on the code below to have the following features → Constructor with all attributes, Builder pattern, Equals, HashCode, and ToString or just @Data to have all features unless the Builder pattern.

Lombok use
Lombok use Log
Test class with Lombok

Code — Records

Step by step.

1 — Enable the preview on pom.xml, since the feature is on preview, [3]A preview feature is a new feature whose design, specification, and implementation are complete, but which is not permanent, which means that the feature may exist in a different form or not at all in future JDK releases.

2 — Create a new Record 💿, yes a new Record not a class 😄. Add the attributes and if any, add some validation. In this Record, only discs released after the 1500s.

Java Record
Record Disc test

Conclusion

At the first glance, the answer is yes, Lombok solves different problems that Records handle and will survive.

But if you don’t use Lombok and every time has to create a new class with just getters/setters Records will help you.

References

1 — https://openjdk.java.net/jeps/359

2 — https://projectlombok.org

3 — https://docs.oracle.com/en/java/javase/15/language/preview-language-and-vm-features.html

4 — https://github.com/luizgustavocosta/luiz-costa-tech/tree/master/records

--

--

Luiz Gustavo De O. Costa
Luiz Gustavo De O. Costa

Written by Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices

No responses yet