Lombok Constructor Annotations
explaination how can make constructor by Lombok annotation.
Options annotation
@AllArgsConstructor, @NoArgsConstructor and @RequiredArgsConstructor
@NoArgsConstructor will generate a default constructor without any parameter.
@AllArgsConstructor will generate a constructor with all parameters in the sequence, they are present in class.
@RequiredArgsConstructor will generate a constructor for only required fields which have @NotNull annotation.
import java.io.Serializable;
import java.util.Date;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Person implements Serializable {
@lombok.NonNull
Long id;
@lombok.NonNull
String name;
Integer Age;
Long phoneNumber;
}
or
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
public class Person{
@lombok.NonNull
Long id;
@lombok.NonNull
String name;
Integer Age;
Long phoneNumber;
}
in the following code with annotations above class declaration i am generated getter, setter, toString,equals and hashCode with default Constructor , Argument Constructor , RequiredArgsConstructor
and we can call constructor by following ways :
Person person=new Person(); // that is default Constructor Person person=new Person(Long id,String name,Integer age, Long phoneNumber); // that is Argu Constructor Person person=new Person(Long id,String name); // that is RequiredArgsConstructor
Conclusion
I Tried explain all side about @AllArgsConstructor, @NoArgsConstructor and @RequiredArgsConstructor annotations and what is the difference between them , and how you can use inside your project you can see Lombok document to see other annotations and you can see following Links in reference section to see more example and explain about lombok annotation .