Lombok
The Lombok Library automatically creates the getter
, setter
, constructor
, toString()
, equals()
, hashCode()
, and many more methods for a Java Class. It does so by providing decorators that can be applied to classes.
Some of the most useful ones are:
@Getter
/@Setter
: Creates getter and setter methods for a field/class.@NoArgsConstructor
/@RequiredArgsConstructor
/@AllArgsConstructor
: Creates constructor with different arguments.@ToString
: Creates atoString()
method.@EqualsAndHashCode
: Createsequals()
andhashCode()
methods.@Data
: Combines all the above decorators. Only@RequiredArgsConstructor
in constructors.
Example:
import lombok.*;
@Data
class Node {
private String foo;
private int bar;
}
/* Usage */
Node n = new Node("baz", 1);
System.out.println(node); // Node(foo='baz', bar=1)