Tuesday, May 7, 2024
HomeJavaFind out how to use Lombok in Java? Lombok @Information and @NonNull...

Find out how to use Lombok in Java? Lombok @Information and @NonNull Annotations Examples


Hi there Java programmers, when you have been doing skilled Java growth, you’ll have heard about Lombok library, one of the common Java libraries amongst skilled Java builders, making coding in Java way more enjoyable and rewarding. Java has all the time been criticized for its verbosity, and lots of programming languages like Scala and Groovy are invented to resolve that downside, however Lombok solves that downside in Java itself. This implies you needn’t be taught a brand new programming language, however you should use the Lombok library and its annotations to put in writing clear and concise code in Java. 

If you happen to love simplicity in your code, then you’re going to love Lombok. It is a small library that’s used to cut back the quantity of boilerplate code in Java courses. Writing constructors, getters, and setters for every class are way more troublesome than specializing in the category’s enterprise logic.

By utilizing Lombok’s easy annotations, you may take away a whole lot of litter kind your Java code which makes your class and code way more readable. This additionally signifies that you simply have to concentrate on the enterprise logic in your class, and Lombok will maintain particulars want by the Java compiler and JVM

For instance, Lombok can robotically generate getters and setters for these objects by utilizing particular Lombok @Information annotations.

import lombok.Information;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Information
public class Pupil {
@Id
@GeneratedValue(technique = GenerationType.IDENTITY)
non-public int id;
non-public int studentId;
non-public String identify;
non-public int age;
}

After including the @Information annotation to your class, you’ll get the next strategies at no cost with out implementing every methodology. Lombok will robotically generate  getId(), getSummary(), getDescription(), setId(), setSummary(), SetDescription(), equals(), canEqual(), hashCode(), toString(), ToDo().

The most effective factor is that Lombok might be built-in with fashionable Java IDEs like IntellijIDEA and Eclipse, which don’t present any error whenever you name these getter and setters on a Information object. They are going to know that Lombok is implementing them at compile time. With out enabling Lombok plugins, IDEs will throw errors like these annoying crimson strains. 

Superior Lombok Options for Java Builders

Lombok supplies many handy annotations to make Java builders simpler. You too can use issues like val, var, @NonNull, @Cleanup, @Information, @Worth, @Builder, @Singular, @Getter(lazy=true), @Log, @UtilityClass, and a slew of different helpful capabilities can be found to any developer. 

You could shorten your code and achieve a excessive degree of automation for normal duties by utilizing these.

The way in which that is working is by auto-generating the java bytecode into your courses everytime you construct.

Let’s have one other class to clarify the Lombok library in java. Right here now we have a Java class with many code strains which have getters, setters, and constructors.

package deal com.instance.studentproject.mannequin;

public class Pupil {

non-public int studentId;
non-public String identify;
non-public int age;

public Pupil(int studentId, String identify, int age) {
  this.studentId = studentId;
  this.identify = identify;
  this.age = age;
}

public int getStudentId() {
  return studentId;
}

public void setStudentId(int studentId) {
  this.studentId = studentId;
}

public String getName() {
  return identify;
}

public void setName(String identify) {
  this.identify = identify;
}

public int getAge() {
  return age;
}

public void setAge(int age) {
  this.age = age;
}

@Override
public String toString() {
return "Pupil{" +
"studentId=" + studentId +
", identify="" + identify + "'' +
", age=" + age +
'}';
}
}

That is how often we write the category in java. Declare the setters, getters, and constructors. However we will scale back the variety of code strains that we used earlier with out decreasing the strategies carried out utilizing Lombok.

import lombok.*;
@ToString 
@Getter 
@Setter
@AllArgsConstructor
public class Pupil {
 non-public int studentId;
 non-public int  age;
 non-public String identify;
 }

We now have carried out the identical performance because the earlier code by utilizing the @ToString, @Getter, and @Setter. And likewise you may add as many Lombok annotations as you wish to robotically generate code. 

How to use Lombok Library in Java? Example Tutorial

When to make use of Lombok and @Information Annotation Examples

Now, let’s have a look at some widespread examples of utilizing the Lombok library in Java growth. We are going to see examples of various Lombok Annotations like @Information and @NonNull, normal @Getter, @Setter, @ToString, and many others.

1. @Information annotation Instance

One fashionable manner of utilizing Lombok is merely including the @Information annotation to your class. This helps to cut back the quantity of code (annotations to characterize every perform) utilizing.

import lombok.Information;

@Information
public class Pupil {
int studentId, age;
String identify;
}

This @Information annotation bundles all of the @Getter / @Setter, @ToString, @EqualsAndHashCode, @Getter / @Setter, and @RequiredArgsConstructor annotations collectively. So, you needn’t use every of those annotations one after the other; you may immediately annotate your knowledge class with @Information, and Lombok will maintain the main points. 

 

2. Test for Null Values – @NonNull Instance

The @NonNull annotation can be utilized to generate a null verify on a setter discipline, which shouldn’t be confused with the Bean Validation annotation. If the annotated class discipline accommodates a null worth, the check-raises a NullPointerException. We are able to merely apply this to a discipline to implement the rule.

@NonNull @Setter
non-public String studentId;

From the above code, you may generate the next code.

public id setStudentId(@NonNull remaining String studentId) {

  if (studentId == null) {
   throw new java.lang.NullPointerException("studentId ");
 }
   this. studentId = studentId;

}

On primitive arguments, @NonNull annotations should not permitted. If they’re, a warning is offered, and no null verify is carried out.

Right here is one other instance of utilizing Lombok in Java code:

How to use Lombok in Java? @Data and @NonNull Annotations Examples

  

That is all about what Lombok library is in Java and find out how to use Lombok to put in writing higher Java code. You could have seen that Lombok annotations can drastically scale back the boiler plate code and make growth quicker and make your code clear and simpler to learn. 

In order a quick, the Lombok in java is just not about decreasing code however about clearly speaking what you need to attain, fairly than having to determine it out from Javadoc or implementations. The decreased code simply makes it simpler to identify any divergent-method implementations.

Different Programming Sources and Articles You could like

Thanks for studying this Java Lombok tutorial thus far. If you happen to like these Lombok @Information and @NonNull annotation examples and clarification then please share them with your folks and colleagues on Fb. If you happen to
have any questions or suggestions then please drop a word.
    
P. S. – In case you are new to the Java world and wish to grasp Java earlier than exploring these instruments and library then you can even try this record of Prime 10 Superior Core Java Programs on Medium. It accommodates totally different Java programs to change into an skilled Java programmer.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments