This question already has an answer here:
- Leaking this in constructor warning 10 answers
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.
This question already has an answer here:
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.
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.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. Leakingthis
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 ifthis
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.