[4983] in java-interest

home help back first fref pref prev next nref lref last post

Re: Creating new objects given a class name

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Thu Jan 25 01:02:18 1996

Date: Wed, 24 Jan 1996 20:07:51 -0800
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: gregg@fruitfly.berkeley.edu
Cc: java-interest@java.Eng.Sun.COM


Hi Gregg,

> I'm trying to take a String with the name of a class, and instantiate 
> objects of that class.  I've been trying to do this with Class methods:
> 
> 	Class class_type;
> 	class_type = Class.forName("class_name");
> 	...
> 	Object obj;
> 	obj = class_type.newInstance();
> 
> This compiles OK, but running it gives a "java.lang.NoSuchMethodError" on 
> that last line.  And even if it did run, I'm not sure what to do with the 
> resulting object -- how do I call a constructor for obj?
> 
> Is there another way to do this?  Am I not _supposed_ to be able to do this?

Yes. But the class that you are trying to instanciate probably does not
have a public default constructor. If you add that then it will work.

Have fun,

	Arthur van Hoff
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post