Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live


Lombok is a java library for automatically creating code for any class. Using this you just have to focus on state information of class.

Lombok provide following utilities in form of annotations. During compilation time it inject code into class bytecode.


  • Please include Lombok jar into your class-path.
  • Please install its plugin from here.

Annotations Levels

Lombok annotation can be used at following level.

  • Type
  • Constructor
  • Method
  • Field
  • Local Variable

Getter & Setter

These will inject getter and setter methods for your class so you don’t have to inject them. It is can be placed at class or field

public class SampleClass{


It will inject more informational toString implementation. It should be placed at class.

public class SampleClass{


Lombok provides three class level annotations for constructors which are as followings


It is similar to default constructor.

public class SampleClass{


It defines constructor with all members.

public class SampleClass{


It  defines constructor with all required members. Required members are those which are final or have another annotation @NonNull.

public class SampleClass{
    private final String name;
    private String email;
   private int age;

// name and email based constructor will be injected


For creating equals and hashcode methods of class. It is class level annotation

public class SampleClass{


It is a class level annotation which combines multiple annotation into one. By use of following annotations can be eliminated.

  • ToString
  • Getter
  • Setter
  • RequiredArgsConstructor
  • EqualsAndHashCode
public class SampleClass{

// Both have same impact

public class SampleClass{


It is a class level annotation which provide immutable funcionality to class.

public class SampleClass{

It povides all argument constructor so that once state of instance is initialsed it cannot be modifed as it only provide getters.

Advance Level Annotations



It is similar to synchronized keyword, but synchronized keyword locks complete object means locking on this. Lombok implements it using synchronized block for method body. It works on both static and instance methods.

Internally it locks Lombok defined properties ($lock for instanceMethod & $LOCK for static methods ) to facilitate synchronisation.

It should be used with any method of class

public class SampleClass{

  public void someMethod() {
    System.out.println("Hello World");


It uses builder design pattern. It will inject a static private class into annotated class to provide complex builder functionality. It also inject a static method builder into class.

public class SampleClass{

How to use

SampleClass obj = SampleClass.builder()


Enjoy writing clean code from now onwards. Thanks

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