How to use Lombok @Value and @RequiredArgsConstructor in Java

What is Lombok? Project Lombok is a java library that automatically plugs into your editor and builds tools, does a lot of work for you to speed up your programming in Java.

It is a great library that can help you avoid writing boilerplate code and saves you precious time.

Table of Content :

What is Lombok @Value annotation?

  • @Value is the replacement for @Data annotation. @Data annotation comes with more annotation with it.
  • If we are using @Value annotation at the class level, then we have no need to use @ToString, @EqualsAndHashCode, @AllArgsConstructor, @FieldDefaults, and @Getter annotations.
  • When we use @Value then on the class level then, all fields are made private and final by default.
  • If we are using @Value annotations on the class level, we don't need to use a private modifier as @Value attaches a private modifier for each field in the class.

Important points about @Value | What does @value do in Lombok?

  • If we use @Value annotation, by default all fields are final, so we do not need to use the final modifier.
  • If you have to write your own @ToString method then @Value will not generate the ToString for us.
  • If you write your own constructor, Lombok will not generate any constructor for you.
  • If you have your own constructor, and you want to generate an AllArgument constructor for yourself, you need to use @AllArgsConstructor.
  • If you want to make the variable final, you need to use the @NonFinal annotation.
  • If you want to make the class nonfinal you can use @NonFinal on the class level as well.

What is Lombok @RequiredArgsConstructor annotation

The @RequiredArgsConstructor generates the constructor with 1 parameter for each field.

When we use @RequiredArgsConstructor annotation on the class level;, then the fields marked with @NonNull, an explicit null check is also provided.


Important points about @RequiredArgsConstructor

  • We can replace Springs @Autowired annotation with @RequiredArgsConstructor.
  • For that, we need to add @RequiredArgsConstructor in the class where we are using @Autowired annotation.
  • If any of the parameters intended for the fields marked with @NonNull contain null, the Constructor will throw a NullPointerException.

If you have not configured Lombok in eclipse or STS, please read the Article Configure Lombok in Eclipse and STS


Lombok @Value and @RequiredArgsConstructor example

In the following example, we can see. there is no error for

Student stud2 = new Student(2, "John", "clay");

as we used @RequiredArgsConstructor on Student class.

Student.java

package com.bala.java.lombok.examples; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.Value; @Value @Setter @RequiredArgsConstructor class Student { int rollNumber; String firstName; String lastName; }

Main class: StudentTestApp.java

package com.bala.java.lombok.examples; public class StudentTestApp { public static void main( String[] args ) { Student stud1 = new Student(1,"Sara","Kenny"); System.out.println("=========Student1 details============"); System.out.println(stud1.toString()); System.out.println("=========Student2 details============"); Student stud2 = new Student(2, "John", "clay"); System.out.println(stud2.getRollNumber()); System.out.println(stud2.getFirstName()); System.out.println(stud2.getLastName()); } }

output:
=========Student1 details============ Student(rollNumber=1, firstName=Sara, lastName=Kenny) =========Student2 details============ 2 John clay

Also, we have @Value annotation on the Student class, as result, we have no error for Getter, methods, and ToString() methods.

In this article we have seen, What are Lombok @Value and @RequiredArgsConstructor annotations? Important points about Lombok @Value and @RequiredArgsConstructor annotation and example as well.