In Java, you can use various libraries to make your code more concise and understandable. One of these libraries is the Lombok library. Lombok allows you to automatically generate some common coding patterns. This way, you avoid writing boilerplate (repetitive) code. Now, let's explain the Lombok annotations used in your code:
@Getter
This annotation automatically creates getter methods (access methods) for all fields of the class. For example, it creates the public String getName()
method for the field private String name;
. In this way, you do not need to write the getter method manually.
@Setter
This annotation automatically creates setter methods (value assignment methods) for all fields of the class. For example, it creates the public void setName(String name)
method for the field private String name;
. In this way, you do not need to write the setter method manually.
@ToString
This annotation automatically creates the toString
method of the class. The toString
method returns a String representing all fields of an object. This is very useful for easily printing and debugging the contents of the object.
@AllArgsConstructor
This annotation creates a constructor method that takes all fields of the class as parameters. That is, it allows you to create all fields with initial values. This allows you to quickly create objects without having to manually assign each field of the class one by one.
To summarize, thanks to these annotations, getter and setter methods for all fields of the Customer
class, toString
method and a constructor method containing all fields are automatically created. This makes your code cleaner and easier to maintain. For example:
@Getter
@Setter
@ToString
@AllArgsConstructor
public class Customer {
private Long id;
private String name;
private String surname;
private String email;
private String password;
private Integer credit;
private String phoneNumber;
private Set<Address> addresses;
private Boolean isActive;
private AccountType accountType;
}
The above code is automatically expanded to look like this:
public class Customer {
private Long id;
private String name;
private String surname;
private String email;
private String password;
private Integer credit;
private String phoneNumber;
private Set<Address> addresses;
private Boolean isActive;
private AccountType accountType;
// Getter and Setter methods for all fields
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// ... other getters and setters
// toString method
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", credit=" + credit +
", phoneNumber='" + phoneNumber + '\'' +
", addresses=" + addresses +
", isActive=" + isActive +
", accountType=" + accountType +
'}';
}
// AllArgsConstructor
public Customer(Long id, String name, String surname, String email, String password, Integer credit, String phoneNumber, Set<Address> addresses, Boolean isActive, AccountType accountType) {
this.id = id;
this.name = name;
this.surname = surname;
this.email = email;
this.password = password;
this.credit = credit;
this.phoneNumber = phoneNumber;
this.addresses = addresses;
this.isActive = isActive;
this.accountType = accountType;
}
}
As you can see, you can significantly reduce the amount of code you need to write by using Lombok annotations.
Top comments (0)