Re: convenience constnuctors
Roedy Green wrote:
Lets say you had a constructor with a number of parameters. You want
ed a version of it that did something to compute half a dozen of them
first.
How do you code that?
Best thing I can recommend would be the Builder pattern, as Todd
recommended. It's the cleanest thing. Either that or the traditional
static makeObject method.
You can't put any calls prior to super
In Java, no. In Java bytecode, yes. By the way, are you talking about
subclassing another class or overloading a constructor?
You don't want to write a separate function for every parameter since
one method computes them all.
And this is the red warning light in my mind. The only implementation I
can think up is either very ugly or not thread-safe.
Would you use a static method?
Being all so esoteric, I'm going to say "a static method creeps in there
at some point." The static method might be makeObject or
computeParamValueFromArg, though. Without more explicit definitions,
though, I can't say...
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth