top | item 43874744

(no title)

_old_dude_ | 10 months ago

In Java, constants are declared as static final.

  static final Complex CONSTANT = new Complex(1, 2);
If you want a lazy initialized constant, you want a stable value

  static final StableValue<Complex> STABLE_VALUE = StableValue.of();

  Complex getLazyConstant() {
    return STABLE_VALUE.orElseGet(() -> new Complex(1, 2))
  }
If you want the fields of a constant to be constant too, Complex has to be declared has a record.

discuss

order

No comments yet.