Dynamic instantiation is part of the Reflection API. You can find more
information in the java tutorial:
http://java.sun.com/docs/books/tutorial/reflect/index.html

It is in fact a very common approach in Java. E.g. I used it once in
combination with the factory pattern. I pulled the class names from a
configuration file and dynamically instantiated them. Very handy to avoid
changing your factory code every time a class is added.

Nordin Haouari

----- Original Message -----
From: "Brian Kotek" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 10, 2004 4:24 PM
Subject: RE: [CFCDev] OT: Dynamic class instantiation in Java?


> Thanks Matt.  If you have it handy, I'd be interested to see the
> additional code needed to pass arguments to the constructor.  If it's a
> hassle then don't worry about it.
>
> Overall, is this a common approach in Java, or is dynamic instantiation
> frowned upon?
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of Matt Liotta
> Sent: Monday, May 10, 2004 10:14 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] OT: Dynamic class instantiation in Java?
>
> Class clazz = Class.forName("package.ClassName");
> clazz.newInstance();
>
> If you want to actually pass parameters to the constructor then there
> is a whole lot more code needed.
>
> Matt Liotta
> R337 Consulting LLC
> http://r337.com
>
>
> On May 10, 2004, at 10:06 AM, Brian Kotek wrote:
>
> > Off topic but I thought someone here might know the answer.  Is it
> > possible to dynamically instantiate a class in Java?  Similar to how
> > you
> > could do:
> >
> > <cfset myObjVar = "someComponent" />
> > <cfset myObj = createObject( 'component', '#myObjVar#' ) />
> >
> > In other words, to instantiate a class in Java when you won't know
> > until
> > runtime what class you want to instantiate?
> >
> >
> > Thanks,
> >
> > Brian
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email
> > to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> > in the message of the email.
> >
> > CFCDev is run by CFCZone (www.cfczone.org) and supported
> > by Mindtool, Corporation (www.mindtool.com).
> >
> > An archive of the CFCDev list is available at
> > www.mail-archive.com/[EMAIL PROTECTED]
> >
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
> An archive of the CFCDev list is available at
> www.mail-archive.com/[EMAIL PROTECTED]
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
> An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]
>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to