Articles

Java for a lot of time has been accused and mocked for its verbosity. Even the most passionate Java developers have to admit that it felt ridiculous to declare a bean class with two attributes. If you follow the right recommendations, you end up adding not only getters and setters, but also the implementations of toString hashcode and equals methods. The final result is a chunk of boilerplate that invites you to start learning another language. 

Java

 

import java.util.Objects; public class Car { private String brand; private String model; private int year; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return "Car{" + "brand='" + brand + ''' + ", model='" + model + ''' + ", year=" + year + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Car car = (Car) o; return year == car.year && Objects.equals(brand, car.brand) && Objects.equals(model, car.model); } @Override public int hashCode() { return Objects.hash(brand, model, year); }
}

Source de l’article sur DZONE