Computed Read Only Properties

Estimated time to read: 1 minute

In order to have a readable property, we need a getter method. The getter, int getAge(), does not expose how we are finding and returning the value.

Our particular implementation uses the new Java 8 classes, but we could have done it some other way.

We added a manufactured attribute to contain the date on which the car were made, and implemented the age property using the new Java 8 Time package, but we could have done it differently, so long as we yielded the same result to the caller.

 private LocalDate manufactured;

 public int getAge() {
  return Period.between(manufactured, LocalDate.now()).getYears();
 }

Attributes and methods... A JavaBean property is, in a sense, neither. It is an abstract notion derived from the presence of certain methods. It feels like data, but it is not an attribute. It is managed by code that has required method signatures, but it feels more like data. Either way, JavaBean properties are essential to many of the frameworks we want to use, including Spring.