POSTS
Overridable Method Calls From the Constructor
PMD and NetBeans both give warnings about calling non-final non-private methods from the constructor of a class.
I never really understood why it was such a bad thing until today. I was hit by the problem it causes when subclassing one of the swing model classes.
public class CustomButtonModel extends DefaultButtonModel {
private final SomeClass object;
public CustomButtonModel(SomeClass object) {
if (object == null)
throw new IllegalArgumentException("null object is a nono");
this.object = object;
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled && object.evaluate());
}
}
}
And i was getting a NullPointerException
in setEnabled
which should be impossible if you just look at the class. Running a debugger I could confirm that object was null but it took quite a while to figure out that DefaultButtonModel
was calling setEnabled()
from its default constructor before object had been assigned.