I have an inner helper class, and I would like to add each new instance of it to a map in the containing class, like this:
public class SomeService {
private final Map<Integer, ServiceTask> pendingTasksByKey;
private class ServiceTask {
private ServiceTask(int key) {
// initialization...
pendingTasksByKey.put(key, this);
}
}
// the rest of the code follows
}
When I do it, NetBeans complains about using this
in the constructor. OK, I get it, it's a dangerous practice in the general case because someone could extend my class and then I would be leaking this
referring to an incompletely initialized object. I didn't want to turn off this warning, so I thought that I could make the class final
. This way no one would be able to extend my class, and therefore it should be pretty safe to use this
, as the initialization is complete as this point. But NetBeans still shows the warning even if I mark the inner class as final
.
Am I right or is there something I missed? Is it just NetBeans being too picky? Besides possible multi-threading memory model issues I can't think of any dangers of such this
usage.