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) { = id;

    public String getName() {
        return name;

    public void setName(String 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 –

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 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 –

public class Employee {
 private String id;
 private String name;


@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.

public class ValueExample {
    String name;
    String id;


@Builder produces complex builder APIs for your classes.

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

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()


@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();


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

Properties properties = new Properties();
InputStream in = new FileInputStream("application.conf");
String key = properties.getProperty("private_key");"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 !!



One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s