Re: Why can't I assign "this" in a constructor?
From: Tor Iver Wilhelmsen (tor.iver.wilhelmsen_at_broadpark.no)
Date: 27 Dec 2003 12:13:26 +0100
firstname.lastname@example.org (Thinkit) writes:
> It seems like a basic thing you'd want to do. It seems it only lets
> you call an alternate constructor, this(...), as the first line in the
> method. I want to be able to just say this=new MyObject(...) or
Because constructors are used from the outside: If the caller wants a
particular MyObject they can call the proper constructor.
An calling this() is't calling an _alternate_ constructor, but is
known as constructor chaining. The constructor you're in is still
_the_ constructor invoked.
> I could just use a static method, but why don't I have the choice?
Because it's insane? You're trying to extend a constructor's role to
being a factory method, which is not what it's for: It's role is to
initiate *one* particular instance, referenced via the value this.