Re: Complex object graph and "this" escaping constructor.
Roedy Green wrote:
On Sun, 28 Oct 2007 13:42:05 -0700, Daniel Pitts
<newsgroup.spamfilter@virtualinfinity.net> wrote, quoted or indirectly
quoted someone who said :
So, I know why it is bad for the "this" reference to escape the
constructor, and that many people advocate using a static factory to
help alleviate this problem, but it seems to me that you could run into
the same kind of trouble.
There is a similar problem of calling virtual methods in constructors
that may access uninitialised (but zeroed) fields.
I think what Java aims to do is prevent you from doing anything that
will crash the JVM. However, you are free to write programs that give
meaningless results.
The idea is they will put in a safety net whereever it is easy to do
so, but feel no guilt about allowing you to write silly code.
Calling possibly overridden methods is the (maybe not obviously) same as
allowing a this reference to escape.
In any case, I'm trying to avoid writing silly code. To me, silly means
code that is too complex and too defensive, or code that is to
simplistic and not "correct".
Thanks,
Daniel
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>