Why do IDE's complain about "leaking this in constructor"? I've always assumed that it's just bad practice. But I actually never found why it is bad.
Leaking the this
reference in the constructor (not controller) is dangerous, especially in a multithreaded environment. This is because the object is not fully constructed until the constructor call finishes. Leaking this
from the constructor thus means that the external world gets access to an object which is not yet fully constructed. This may not necessarily lead to problems in a a single-threaded program (although it is possible, but the problem is much more obvious in this case). But if this
is leaked to other threads, they can actually try to do something with the object before its construction is finished, which leads to subtle and hard to find bugs.
There are few absolutes in life, eg. you must pay taxes ... or ... death is inevitable. But "passing this
out of a constructor is always bad" is -not- one of them.
The caveats pointed out by Peter are all apt and valid. It would certainly be problematic to leak this
from a constructor into any method or context in which the reference would become published to unknown or untrusted clients. It is still bad to publish a reference for a not-yet-fully-constructed object to any client code, trusted or not, that operates with the assumption that it will have a view to a valid and consistent object.
That said, there is absolutely nothing wrong with passing this
from a constructor to a package-private method that performs a common initialization on, say, a group of objects that share a common interface, particularly if that initialization is lengthy or complex.
TL;DR: There are certainly some situations in which, in my opinion, it is not only acceptable to pass this
from a constructor, but actually desirable to do so.
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