Home >Java >javaTutorial >Transform Your Java Code: Unlock the Power of Immutability with Lombok in Just Minutes!
Immutable classes are beneficial because they are inherently thread-safe, easy to reason about, and prevent accidental changes to the object's state. An immutable object’s state cannot be modified after it is created, making it a valuable design pattern, especially in multi-threaded environments.
Consider the following Employee class:
final class Employee { private final long id; private final String name; private final double salary; public Employee(long id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public long getId() { return id; } public String getName() { return name; } public double getSalary() { return salary; } }
In this traditional approach:
While this approach works well, it involves writing boilerplate code for constructors, getters, and sometimes equals, hashCode, and toString methods.
Lombok can drastically reduce the amount of code you need to write. Here's how you can achieve the same functionality with Lombok:
import lombok.AllArgsConstructor; import lombok.Getter; @AllArgsConstructor @Getter final class Employee { private final long id; private final String name; private final double salary; }
This version uses Lombok annotations to generate the constructor and getters automatically:
Lombok’s @Value annotation is a more powerful alternative that combines multiple features to create an immutable class:
import lombok.Value; @Value class Employee { long id; String name; double salary; }
With @Value, Lombok automatically:
This reduces your class definition to just the fields, with all the necessary code automatically generated.
Immutable objects do not allow modification of their state. However, in certain cases, you may need to create a modified copy of the object, such as updating an employee's salary. Without Lombok, this could look like:
@Value class Employee { long id; String name; double salary; } class Main { public static void main(String... args) { var emp = new Employee(1L, "Aman", 10_000.0); emp = updateSalary(emp, 12_0000.0); } public Employee updateSalary(Employee emp, long newSalary) { return new Employee(emp.getId(), emp.getName(), newSalary); } }
This is straightforward but tedious, especially when dealing with classes that have many fields.
Lombok's @With annotation simplifies this:
import lombok.Value; import lombok.With; @Value class Employee { long id; String name; @With double salary; } class Main { public static void main(String... args) { var emp = new Employee(1L, "Aman", 10_000.0); emp = updateSalary(emp, 12_0000.0); } public Employee updateSalary(Employee emp, double newSalary) { return emp.withSalary(newSalary); } }
The @With annotation generates a method that returns a new instance of the class with the specified field updated, leaving the rest unchanged.
The de-lomboked version of our Employee class (i.e., what Lombok generates under the hood) would look like this:
final class Employee { private final long id; private final String name; private final double salary; public Employee(long id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public Employee withSalary(double salary) { return this.salary == salary ? this : new Employee(this.id, this.name, salary); } public long getId() { return this.id; } public String getName() { return this.name; } public double getSalary() { return this.salary; } @Override public boolean equals(final Object o) { if (o == this) return true; if (!(o instanceof Employee)) return false; final Employee other = (Employee) o; if (this.getId() != other.getId()) return false; final Object this$name = this.getName(); final Object other$name = other.getName(); if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false; return Double.compare(this.getSalary(), other.getSalary()) == 0; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final long $id = this.getId(); result = result * PRIME + (int) ($id >>> 32 ^ $id); final Object $name = this.getName(); result = result * PRIME + ($name == null ? 43 : $name.hashCode()); final long $salary = Double.doubleToLongBits(this.getSalary()); result = result * PRIME + (int) ($salary >>> 32 ^ $salary); return result; } @Override public String toString() { return "Employee(id=" + this.getId() + ", name=" + this.getName() + ", salary=" + this.getSalary() + ")"; } }
While Lombok simplifies creating immutable classes, it's important to note some potential pitfalls:
While immutability offers significant benefits, it’s important to consider the performance impact, particularly in scenarios involving frequent updates:
Lombok's @Value and @With annotations offer a powerful and concise way to create immutable classes in Java, eliminating the need for boilerplate code and making your code more readable and maintainable. By leveraging these annotations, you can focus on the logic of your application rather than the mechanics of class design.
The above is the detailed content of Transform Your Java Code: Unlock the Power of Immutability with Lombok in Just Minutes!. For more information, please follow other related articles on the PHP Chinese website!