Is there something that I can do to simulate "on created" event?
I have a base class which other people can extend, and currently I have an init()
that will initialize the instance, but it should be called only after the constructor stack completes.
If I call init()
in the base class' constructor, then I might potentially initialize wrongly, because the subclass' constructor has not finished executing. The subclass can initialize values of some of the protected fields, which would have an impact on how init()
initializes the instance.
My current approach is to make init()
protected final
, and asks that subclasses must call it at the end of their constructor. This approach works, but only provided if subclasses really do follow the instruction.
Edit
I thought I would give some extra information. This is part of a JavaFX project/custom API. So I had tried using Platform.runLater(() -> init())
, which was a major mistake. Platform.runLater()
would only run after the whole execution stack completes, which causes the usage of uninitialized instance in this case:
Foo instance = new Foo();
instance.doSomething();