wrote:
> Can you comment on how I've done it in the code provided below? I would
> like to know if there is a better way. Perhaps to have the program get
> the list of constructors and then figure out how to get the correct
> parameter list. Comments/Suggestions?
Your code seems perfectly reasonable to me. That's the way I'd have coded it
myself
Just two minor observations:
You have chosen to use getDeclaredConstructor() rather than getConstructor(),
the difference is that getConstructor() only looks for public constructors.
For some purposes that would be the better option, though it doesn't really
make any difference for exercises like this (especially as the two Toy
constructors are not public !).
(If this paragraph confuses you then please just ignore it) Since JDK 1.5 you
don't need to create the parameter arrays explicitly. You can just say:
Constructor constructor = c.getDeclaredConstructor(int.class);
o2 = constructor.newInstance(999);
It's important to realise that all that's happening is that under the hood the
compiler is generating exactly the same code as you originally wrote, it's just
that those two methods have been made "varadic" -- which means that they take a
variable list of arguments which the compiler packs into an array for you.
-- chris