how to create object by getting class name as input

hi

i need to create object for the existing classes by getting class name as input from the user at run time.

how can i do it.

for exapmle ExpnEvaluation is a class, i will get this class name as input and store it in a string

String classname = "ExpnEvaluation";

now how can i create object for the class ExpnEvaluation by using the string classname in which the class name is storted

Thanks in advance

[448 byte] By [azhara] at [2007-11-27 5:45:03]
# 1

String classname = "ExpnEvaluation";

Object obj = Class.forName(classname).newInstance();

then you need to cast your new obj to the type you want;

note that you have to write the full package path class name, and your class must be in your class path.

Good Luck

Ahmad Elsafty

NourElsaftya at 2007-7-12 15:26:21 > top of Java-index,Java Essentials,Java Programming...
# 2

String classname = "ExpnEvaluation";

Object obj = Class.forName(classname).newInstance();

using this caode i can able to create object but how can i call the methods of the class "ExpnEvaluation" using this object.

i think we have to cast it, can u help me how to cast the obj to the classname that i get as input

azhara at 2007-7-12 15:26:21 > top of Java-index,Java Essentials,Java Programming...
# 3

String classname = "ExpnEvaluation";

Object obj = Class.forName(classname).newInstance();

using this caode i can able to create object but how can i call the methods of the class "ExpnEvaluation" using this object.

i think we have to cast it, can u help me how to cast the obj to the classname that i get as input

azhara at 2007-7-12 15:26:21 > top of Java-index,Java Essentials,Java Programming...
# 4
> i think we have to cast it, can u help me how to cast> the obj to the classname that i get as input... or use the reflection API. See package java.lang.reflect
quittea at 2007-7-12 15:26:21 > top of Java-index,Java Essentials,Java Programming...
# 5
u can make custome ClassLoader to Trace the Classes in your project
eaajea at 2007-7-12 15:26:21 > top of Java-index,Java Essentials,Java Programming...
# 6

> i think we have to cast it, can u help me how to cast

> the obj to the classname that i get as input

That's exactly the point why you shouldn't be doing this. You can't have a dynamic cast at compile time already. It doesn't make sense. Which is also the reason why class.forName().newInstance() is a very nice but also often very useless tool, unless the classes loaded all share a mutual interface.

CeciNEstPasUnProgrammeura at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 7

That's exactly the point why you shouldn't be doing this. You can't have a dynamic cast at compile time already. It doesn't make sense. Which is also the reason why class.forName().newInstance() is a very nice but also often very useless tool, unless the classes loaded all share a mutual interface.

then what can i do do create object by getting class name as input

azhara at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 8
use Reflection
eaajea at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 9

> then what can i do do create object by getting class

> name as input

Find a way to do it by doing something different. Like a Hashmap full of functor instances that all have a Runner interface with a runMe() method, for example. I can't tell you what to do since I don't know your requirements.

CeciNEstPasUnProgrammeura at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 10

Here's an example (not tested):

Object o = Class.forName(yourClassName).newInstance();

Method m = o.getClass().getMethod(yourMethodName, new Object[] {/* your parameter types */});

Object result = m.invoke(o, new Object[] {/* your parameters */});

quittea at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 11
> use ReflectionWhich of course means keeping meta-data for each class: name, and which method to call after instantiating it, and what the arguments and return values might be... which you then still can't handle. :p
CeciNEstPasUnProgrammeura at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 12

> Here's an example (not tested):

> > Object o =

> Class.forName(yourClassName).newInstance();

> Method m = o.getClass().getMethod(yourMethodName, new

> Object[] {/* your parameter types */});

> Object result = m.invoke(o, new Object[] {/* your

> parameters */});

>

Close. getMethod takes an array of Classes :-)

georgemca at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 13
> Close. getMethod takes an array of Classes :-)My inmind-compiler uses a more tolerant type-check and magical auto-casts ;)
quittea at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 14
> > Close. getMethod takes an array of Classes :-)> > My inmind-compiler uses a more tolerant type-check> and magical auto-casts ;)Nice. Plzzzzz where to downloads inmindcompiler dynamically, I have doubt plzzzz thankyou sir
georgemca at 2007-7-12 15:26:22 > top of Java-index,Java Essentials,Java Programming...
# 15
> Nice. Plzzzzz where to downloads inmindcompiler> dynamically, I have doubt plzzzz thankyou sirI'm sorry, but your locale is not supported yet. Please register the newsletter from vaporware.biz in order to get all the latest news about our development progress.
quittea at 2007-7-21 21:34:24 > top of Java-index,Java Essentials,Java Programming...
# 16

> > Nice. Plzzzzz where to downloads inmindcompiler

> > dynamically, I have doubt plzzzz thankyou sir

>

> I'm sorry, but your locale is not supported yet.

> Please register the newsletter from vaporware.biz in

> order to get all the latest news about our

> development progress.

Thankyou dear but I need code samples and explnatin urgently plzzzzzzz

georgemca at 2007-7-21 21:34:24 > top of Java-index,Java Essentials,Java Programming...
# 17

> Thankyou dear but I need code samples and explnatin urgently plzzzzzzz

Sorry gentleman, I didn't know it was ugrent. Here you go:

Result res = null;

try {

res = InmindCompiler.newInstance().compile(/* your stuff here */);

}

catch (BlownUpException ex) {

/* never mind, happens once in a while */

}

catch (SleepModeException ex) {

/* ignore; state should be polled in a loop or use timer to notify */

}

catch (Exception ex) {

res = new PerfectResult(/* your stuff here */, PerfectResult.MAGIC_ON);

}

/* done */

quittea at 2007-7-21 21:34:24 > top of Java-index,Java Essentials,Java Programming...