Class newInstance() with parameters

Is it possible to use newInstance to create an instance of a Class where the constructor is expecting parameters? I know the API lists newInstance as having no parameters and calling essentially a no-arg constructor, but is there any type of design that would allow me to create an instance with parameters?

[314 byte] By [Bart69a] at [2007-11-27 10:49:27]
# 1

Class.getDeclaredConstructor()

Constructor.newInstance()

CeciNEstPasUnProgrammeura at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 2

Although that's just code, and rather anti-design IMO. Although I don't know what you need and why you might need it, so I can't suggest a different way. It might be the most suitable way for you.

CeciNEstPasUnProgrammeura at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 3

The previous architect went hogwild with reflection so now I have to wrestle with it at every turn. At the point I need to instantiate an object, all I know is the String representation of its name. However, I want to create the object with parameters in its constructor. I'd rather do this a different way, but the architect's questionable love for reflection is forcing me to do otherwise.

Bart69a at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 4

> The previous architect went hogwild with reflection

> so now I have to wrestle with it at every turn. At

> the point I need to instantiate an object, all I know

> is the String representation of its name. However, I

> want to create the object with parameters in its

> constructor. I'd rather do this a different way, but

> the architect's questionable love for reflection is

> forcing me to do otherwise.

If all you have is the classname as a string, you have little choice but to use reflection. But is that actually the case?

georgemca at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 5

> The previous architect went hogwild with reflection

He wasn't the ******* that designed Spring, was he?

BigDaddyLoveHandlesa at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 6

> > The previous architect went hogwild with

> reflection

>

> He wasn't the ******* that designed Spring, was he?

Screw you hippie, Spring rawks!

georgemca at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 7

> > > The previous architect went hogwild with reflection

> >

> > He wasn't the ******* that designed Spring, was he?

>

> Screw you hippie, Spring rawks!

See you in hell, reflection boi!

BigDaddyLoveHandlesa at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > > The previous architect went hogwild with

> reflection

> >

> > He wasn't the ******* that designed Spring, was

>he?

>

> > Screw you hippie, Spring rawks!

>

> See you in hell, reflection boi!

I doubt it. I'm going to sneakily call setAccessible(true) on Heaven

georgemca at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 9

> > > > > The previous architect went hogwild with reflection

> > >

> > > He wasn't the ******* that designed Spring, was he?

> >

> > > Screw you hippie, Spring rawks!

> >

> > See you in hell, reflection boi!

>

> I doubt it. I'm going to sneakily call setAccessible(true) on Heaven

The Devil is my SecurityManager :-(

At least I won't be going to the level reserved for applet coders...

BigDaddyLoveHandlesa at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > > > > The previous architect went hogwild with

> reflection

> > > >

> > > > He wasn't the ******* that designed Spring, was

> he?

> >

> > > Screw you hippie, Spring rawks!

> >

> > See you in hell, reflection boi!

>

> I doubt it. I'm going to sneakily call

> setAccessible(true) on Heaven

>

> The Devil is my SecurityManager :-(

> At least I won't be going to the level reserved for

> applet coders...

Old Nick certainly is busy these days, what with that, owning all the best tunes and, apparently, writing Eclipse

georgemca at 2007-7-29 11:19:23 > top of Java-index,Java Essentials,Java Programming...