<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Say “bye bye!!!”​ to Annoying Getters&#x2F;Setters &amp; Shorten your Java Code with &#96;​ lombok &#96;]]></title><description><![CDATA[<p dir="auto"><img src="/assets/uploads/files/1561548435115-capture-resized.png" alt="0_1561548435753_Capture.PNG" class=" img-responsive img-markdown" /><br />
Still, there are things that a Java programmer has to do over and over again just to get the code complete from language/syntax point-of-view rather than spending time on business logic, the actual task at hand. <strong>Having to write or even auto-generate Getters ,Setters, custom Constructors and custom toString method in POJOs is one such issue.</strong> As a solution for this cumbersome problem,  Project Lombok has quite elegantly addressed this issue.</p>
<p dir="auto">First, just need to add the lombok dependency to your project and then use the available annotations appropriately. In your Spring boot project, go to <em><strong>pom.xml</strong></em>  file and add the below dependency within the tags <strong>&lt;dependencies&gt;...&lt;/dependencies&gt;</strong></p>
<pre><code>&lt;dependency&gt;
   &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
   &lt;artifactId&gt;lombok&lt;/artifactId&gt;
   &lt;optional&gt;true&lt;/optional&gt;
   &lt;version&gt;1.18.8&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<p dir="auto">Check for the most recent available version <a href="https://projectlombok.org/changelog" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>.</p>
<p dir="auto">For the latest <a href="https://projectlombok.org/download" target="_blank" rel="noopener noreferrer nofollow ugc">lombok.jar</a> file</p>
<p dir="auto"><strong>1.0 Annotations Available for Use</strong></p>
<p dir="auto">Lombok project provides us with several annotations to avoid boilerplate codes in our entity class. Given below is a list of the available annotations and some very essential set of the annotations will be discussed briefly with examples.</p>
<pre><code>        *Annotation List*
</code></pre>
<p dir="auto">@Getter  @Setter  @Builder   @NonNull</p>
<p dir="auto">@Data    @ToString         @Getter(lazy=true)                  @AllArgsConstructor</p>
<p dir="auto">@NonNull               @EqualsAndHashCode</p>
<p dir="auto">@NoArgsConstructor           @Value  @Log       @Synchronized</p>
<p dir="auto">@RequiredArgsConstructor       @SneakyThrows   @Cleanup</p>
<p dir="auto">@ToString.Exclude             @ToString.Include<br />
@EqualsAndHashCode.Exclude</p>
<p dir="auto">=========================================================================</p>
<p dir="auto"><strong>@Getter/@Setter</strong></p>
<p dir="auto">When you annotate your Entity class with <em>@Getter</em> and <em>@Setter</em> , it will generate getter methods and setter methods for all the fields in your class.</p>
<p dir="auto"><strong>Or</strong>, you can place these two annotations separately before the necessary fields (Not before the class name) ,when you don't need getting and setting methods for all the fields available.</p>
<p dir="auto"><img src="/assets/uploads/files/1561548587009-757cfdd1-785e-4990-9ff5-664bb9f11380-image-resized.png" alt="0_1561548586703_757cfdd1-785e-4990-9ff5-664bb9f11380-image.png" class=" img-responsive img-markdown" /></p>
<hr />
<p dir="auto"><strong>@NoArgsConstructor</strong></p>
<p dir="auto">When you place this constructor before the class name, this will create a constructor without any arguments in it.(an empty constructor)</p>
<p dir="auto"><img src="/assets/uploads/files/1561548644474-02b31c37-bd7a-42a1-88c5-b0faef078895-image.png" alt="0_1561548645658_02b31c37-bd7a-42a1-88c5-b0faef078895-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">This annotation is useful primarily in combination with either @Data or one of the other constructor generating annotations.</p>
<hr />
<p dir="auto"><strong>@AllArgsConstructor</strong></p>
<p dir="auto">This annotation when place before the class name, generates a constructor taking all the fields as arguments in it.</p>
<p dir="auto"><img src="/assets/uploads/files/1561548696107-d2f49d31-ba1c-47ec-b674-dd73f60f4dc9-image-resized.png" alt="0_1561548696953_d2f49d31-ba1c-47ec-b674-dd73f60f4dc9-image.png" class=" img-responsive img-markdown" /></p>
<hr />
<p dir="auto"><strong>@NonNull</strong></p>
<p dir="auto">You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.(See below code examples)</p>
<p dir="auto">** Using @NonNull annotation **</p>
<pre><code>package com.udith.articles.lombokDemo.entities;

import com.udith.articles.lombokDemo.entities.Person;
import java.io.Serializable;

import lombok.NonNull;

public class Student extends Serializable {
  private String name;
  
  public Student(@NonNull Person person) {
    super("Hello");
    this.name = person.getName();
  }
}
</code></pre>
<p dir="auto">** When lombok generates the code for the above snippet, the code looks like this **</p>
<pre><code>package com.udith.articles.lombokDemo.entities;

import com.udith.articles.lombokDemo.entities.Person;
import java.io.Serializable;

import lombok.NonNull;


public class Student extends Serializable {
  private String name;
  
  public Student(@NonNull Person person) {
    super("Hello");

   if (person == null) {
      throw new NullPointerException("person is marked @NonNull but is null");
    }


    this.name = person.getName();
  }

}
</code></pre>
<p dir="auto">Further, you can annotate the fields in an entity class so that those marked fields will be used for creating constructor with the annotations @Data and @RequiredArgsConstructor .</p>
<hr />
<p dir="auto"><strong>@RequiredArgsConstructor</strong></p>
<p dir="auto">@RequiredArgsConstructor generates a constructor with parameters for the fields that requires special handling. All <strong>non-initialized final fields</strong> get a parameter, as well as <strong>any fields that are marked as @NonNull</strong> that aren't initialized where they are declared.</p>
<p dir="auto">See the code snippet below. The commented part shows the lombok generated constructor.<br />
<img src="/assets/uploads/files/1561549118280-4f5c9b40-79ac-45fd-aace-200c2356291c-image-resized.png" alt="0_1561549118069_4f5c9b40-79ac-45fd-aace-200c2356291c-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">When this annotation generates the constructor, the parameters included are ordered in the way the corresponding fields are ordered.</p>
<hr />
<p dir="auto"><strong>@ToString</strong></p>
<p dir="auto">This is also a very useful annotation in debugging etc. Any class definition may be annotated with @ToString to let lombok generate an implementation of the toString() method. By default, it'll print your class name, along with each field, in order, separated by commas.</p>
<p dir="auto"><img src="/assets/uploads/files/1561549163991-1c8cfb91-ea12-462d-9aaf-6b7cff34a46c-image-resized.png" alt="0_1561549165225_1c8cfb91-ea12-462d-9aaf-6b7cff34a46c-image.png" class=" img-responsive img-markdown" /><br />
<strong>@ToString</strong> annotation by default implies <em><strong>@ToString(includeFieldNames = true)</strong></em> .</p>
<p dir="auto">But, by changing its above property to <strong>false</strong>, a string without fields' names can be returned.</p>
<p dir="auto">Further, we can customize the fields that we need to exclude and include to the ToString() method. This can be achieved in two ways.</p>
<p dir="auto"><strong>1) Use of @ToString.Exclude annotation with @ToString annotation.</strong></p>
<p dir="auto">Just add @ToString.Exclude annotation before the fields that you need to exclude from the returned string. (See the example right).<br />
<img src="/assets/uploads/files/1561549306364-cec3a8e9-910e-4305-ab77-2f9e9cf52e6a-image-resized.png" alt="0_1561549307286_cec3a8e9-910e-4305-ab77-2f9e9cf52e6a-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto"><strong>2) Use of @ToString.Include annotation with @ToString(onlyExplicitlyIncluded = true) annotation.</strong></p>
<p dir="auto">After adding @ToString(onlyExplicitlyIncluded = true) before the class, add @ToString.Include before the required fields. Then, only those required fields will be included in the returned string.</p>
<p dir="auto"><img src="/assets/uploads/files/1561549371409-a759b4a8-559b-47d2-badf-b84ae31d080b-image-resized.png" alt="0_1561549372258_a759b4a8-559b-47d2-badf-b84ae31d080b-image.png" class=" img-responsive img-markdown" /></p>
<hr />
<p dir="auto"><strong>@EqualsAndHashCode</strong></p>
<p dir="auto">When placed before a class declaration , this annotation implements public boolean equals(Object obj){}  method for all the fields and public int hashCode(){} method for all the fields.</p>
<hr />
<p dir="auto">You can exclude any field that needn't to be inserted in the generated two methods, simply by placing <strong>@EqualsAndHashCode.Exclude</strong> annotation over the field you need to exclude.<br />
<img src="/assets/uploads/files/1561550156155-3d028d4c-8a7d-48b6-8752-e968a878890d-image-resized.png" alt="0_1561550156747_3d028d4c-8a7d-48b6-8752-e968a878890d-image.png" class=" img-responsive img-markdown" /></p>
<pre><code>  [ Or, you can explicitly include the required fields for these two methods by replacing @EqualsAndHashCode with ** @EqualsAndHashCode(onlyExplicitlyIncluded = true) ** before the class declaration and then placing @EqualsAndHashCode.Include over the required fields. ]
</code></pre>
<hr />
<p dir="auto"><strong>@Data</strong></p>
<p dir="auto">This is a very convenient shortcut key that bundles the features of  @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together.</p>
<pre><code>package com.udith.lombokdemo.entities;


import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.Id;


import lombok.Data;


@Entity
@Data
public class Student implements Serializable {
    
    @Id
    private long id;     
    private int age;
    private Date birthday;

}
</code></pre>
<p dir="auto">===================*******************************============================</p>
<p dir="auto">Above explained are only some frequently used annotations, but you can handle them smoother as you require with lombok. You can find more features <a href="https://projectlombok.org/features/all" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>.</p>
<hr />
<hr />
<p dir="auto">By,<br />
Udith Indrakantha,<br />
Author of <a href="https://webtechmora.blogspot.com" target="_blank" rel="noopener noreferrer nofollow ugc">https://webtechmora.blogspot.com</a></p>
]]></description><link>https://lankadevelopers.lk/topic/286/say-bye-bye-to-annoying-getters-setters-shorten-your-java-code-with-lombok</link><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 22:59:06 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/286.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Jun 2019 12:03:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Say “bye bye!!!”​ to Annoying Getters&#x2F;Setters &amp; Shorten your Java Code with &#96;​ lombok &#96; on Thu, 27 Jun 2019 05:56:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/216">@Udith-Indrakantha</a> Patta</p>
]]></description><link>https://lankadevelopers.lk/post/1792</link><guid isPermaLink="true">https://lankadevelopers.lk/post/1792</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Thu, 27 Jun 2019 05:56:59 GMT</pubDate></item></channel></rss>