I have a pair of classes looking like this;
public abstract class Class1 {
//...
public Class1() {
//...
function2();
//...
}
protected abstract void function2();
}
public class Class2 implements Class1 {
private final OnSomethingListener mOnSomethingListener = new OnSomethingListener() {
@Override
onSomething() {
doThatOtherThing();
}
}
protected void function2() {
//uses mOnSomethingListener
//however mOnSomethingListener is null when this function is called from super()
//...
}
public Class2() {
super();
}
}
I assume the listener is null because I am effectively referencing it from super() and it hasn't instantiated yet. However, I want to make it final because, well, it is. Can I get this field (the listener) to initialize in time without putting it in the superclass (which won't be using the listener, ever)?
Your design is an instance of the "leaked this problem" and is an anti-pattern in Java. You should never call out to a publicly overridable method from a constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With