It’s year 2020, stop writing Getters/Setters in your Java Codes

Ademola Kazeem
3 min readJan 16, 2020

--

Technology is arguably the best thing that ever happened to humanity. One of the things that make Technology exciting is Programming. You have the chance to write a program to help you with your routines, solve world’s problems and make lives easy for people around the world.

Over 3 Billion Devices ‘Run’ Java

Many programming languages exist today but Java programming language has been around since mid-90’s and according to Tiobe, it is has been the most popular programming languages since its creation.

3 Billion Devices “Run” Java

Its popularity is attributed to features including but not limited to: portability, robustness, security, large community, object-oriented nature and Scalability. This is why many of the world’s biggest companies use it to build enterprise applications. Oracle estimated that over 3 Billion Devices “Run” Java, that is how popular the language is.

Java is Verbose

Java is verbose programming language and one of the great features of Java is encapsulation. With Encapsulation, the values or state of a structured data object inside a class are hidden, preventing unauthorised parties’ direct access to them. Publicly accessible methods are generally provided in the class to access the values, and other client classes call these methods to retrieve and modify the values within the object, these are popularly called ‘getters’ and ‘setters’.

public class Person {
//private has restricted access
private String name;

//Getter Method
public String getName() {
return name;
}

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

Getters and Setters, ToString, Hashcodes, Constructors and Equals are all boilerplate codes when creating a model for an object and Java developers have to create these methods every time an object is to be created. The modern Integrated Development Environments (IDEs) have made the creation of these boilerplate code easier, but the times developers spend on creating these boilerplate codes can still be expended on actual development of other important part of a project. How about a library that can help to keep our model classes cleaner and reduce programming time with just an addition of an annotation? Luckily, Project Lombok helps to solve this problem.

Introducing Project Lombok

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

Project Lombok

Lombok dependency helps developers to easily create the domain model
classes and eliminates the boilerplate setters, getters, and other overrides.

It also uses the @Data annotation,
which is a Lombok annotation that generates a default constructor (if you don’t
have one) and all the setters, getters, and overrides, such as the toString method, to make the class cleaner

Project Lombok Usage

  • Download Project Lombok jar file from Project Lombok Download page
  • Add it to your java project
  • If you are using Maven or Gradle, go to the maven repository to get lombok Maven/Gradle dependency
  • Add the dependency to your pom.xml if you are using Maven
  • Add the dependency to your build.gradle file if you are using Gradle
  • Add @Data annotation to your model as shown in the code snippet below:
@Data
public class Users {
private Long id;
private String firstname;
private String lastname;
private String email;
}

Other Project Lombok Features

If you do not want to take advantage of @Data to have access to Getter, Setter, Constructor, ToString and HashCode, you can just annotate the class member with @Getter/@Setter, @toString, @EqualsAndHashCode, etc. Other features can be found on the Project Lombok official website.

Project Lombok really helps make our codes cleaner and reduces developers’ time expended on writing or generating the boilerplate codes. If you have not already started using it, it’s time to give it a try.

--

--

Ademola Kazeem

Vibrant & Articulate Software Engineer | Technologies Enthusiast | Adventurer. https://www.persistentminds.com