public class App {
private final A a;
private final Server server;
public App(){
a = new A(this); //Bad, this is escaping before it's initialized.
}
@Subscribe //This event fires some time after App is finished constructing.
public void registerStuff(RegisterEvent event){
server = event.getServer(); //Not possible due to final field and this not being the constructor, is there such thing as a lazy final?
a.register();
}
}
public class A {
private final App app;
private final BuilderSpec spec;
public A(App app){
this.app = app;
this.spec = app.getServer().builder(this.app).build();
}
public void register(){
app.getServer().doStuff(this.app, this.spec);
}
}
I've read a little about what "this" escaping is and realize that the previous code is bad, as I have little idea what the external processes are doing with the this reference, so it shouldn't be passed outside the constructor until it is constructed.
However, due to the final fields in both App and A, I really don't see how I can initialize this afterwards, or lazily. Is making the fields final less important then the this escaping? I'm not sure.