How to use Immutables to initialize two default fields that depend on each other

14 views Asked by At

I am creating an object using Java Immutables library. I have two fields that needs default values. The default value of field A depends on the value of field B. The default value of field B depends on the value of field A. I also want to assign a default value for the case when A,B are originally null. How can I accomplish this?

According to the Immutables docs,

Since version 2.1, a default attribute initializer method’s body can refer to other default or derived attributes as long as this does not introduce initialization cycles. If a cycle is introduced, then an IllegalStateException will be thrown pointing to attributes which form a cycle.

It seems my arrangement would trigger such a cycle. What should I do? Thanks in advance.

0

There are 0 answers