public class TopLevelObject{
List<SecondLevelObject> secondLevelObjects;
Long field;
}
public class SecondLevelObject{
List<ThirdLevelObject> thirdLevelObjects;
Long field;
}
public class ThirdLevelObject{
Long field1;
Long field2;
}
We have a TopLevelObject, which contains a list of SecondLevelObjects, all of which contain list of ThirdLevelObjects
We need to initialize some fields of this structure initially by iterating through it, and postpone initializing the others, but avoid iterating it all over again
So is it possible to store references/path to the setters of that field to call them later? (Assuming you can know which setter to call when)
Firstly, is this just a matter of performance? Because if so, come back when you’ve measured it.
Second, check you’re not being too clever. Read up about the Inner Platform effect. Do you really need to hand-roll this lazy initialization code, or can you use JPA or something else?
If you’re still sure, you can use a custom object that defers the setting like this
These deferred setter
Runnables can be added to a collection that you would loop through during the later initialization. But I still think you can avoid this approach by simplifying your design.