Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create class from class name

Status
Not open for further replies.

almoes

Programmer
Jan 8, 2003
291
US
Hi,

i want to create something like a class wrapper so that if passed as argument the name and parameters of a class, this wrappers gives back and object instance of this class. How can I do this? I was browsing through the Class class docu, but can't make it work... thanxs!

cheers,
alej
 
I think i got something, but would still appreciate feedback if someone has something to add.
It would be something like:

myCons=Class.forName(classname).getConstructor(Class[] pramTypes);
return myCons.newInstance(Object[] initParams);
 
Like this :

Code:
		Class c = Class.forName("Test");
		Test testObj = (Test)c.newInstance();
		testObj.sayHello();

--------------------------------------------------
Free Database Connection Pooling Software
 
In my case the classname is stored in a variable passed as a parameter so you would not be able to make the cast.
 
You may also want to have a look at reflection, here is an example using a "local newInstance" method to demonstrate it :

Code:
public class Test  {

	public Object newInstance() {
			return new Test();
	}

	public void sayHello() {
		System.out.println("Hello");
	}


	public void test() throws Exception{
		Class c = Class.forName("Test");

		java.lang.reflect.Method method = c.getMethod("newInstance", null);

		Object obj = method.invoke(this, new Object[0]);

		Test testObj = (Test)obj;
		testObj.sayHello();

	}

	public static void main(String args[]) throws Exception {
		new Test().test();
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Still, it doesn't fit, my aim is to create a standard class factory, where you pass as parameter the class name and you get back an object for it.
 
Errm - I was posting some helpful code that you can look at for demonstrating how to call methods on the fly without explicitly calling the method in your code.

If you just want an Object back, then just do this :

Code:
        Class c = Class.forName(className);
        return c.newInstance();

--------------------------------------------------
Free Database Connection Pooling Software
 
I appreciate your feedback, really thanxs!
Regarding your last post, I think that's only possible if the class has no parameters in the constructor.
 
Indeed - this is why you would need to use reflection if you need to pass in constructor args aswell - hence my example !

--------------------------------------------------
Free Database Connection Pooling Software
 
but your example has no parameters in the constructor..
 
Yes, but my example shows you how to invoke methods on the fly - so I was hoping you would be able to look at that, and work out how to do the same with the Constructor !

Never mind, here you go :

Code:
public class Test  {
	public Test(String abc, Integer def) {
		System.out.println("In constructor " +abc +" " +def);
	}

	public Test() {}

	public static void main(String args[]) throws Exception {
		new Test().test();
	}

	public void test() throws Exception{
		Class c = Class.forName("Test");
		java.lang.reflect.Constructor con = c.getConstructor(new Class[] { new String().getClass(), new Integer(0).getClass() });
		Object obj = con.newInstance(new Object[] { "abc", new Integer(500) });
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
And yes, to use reflection, you will need to have a no-arg constructor, I don't believe you can use it without.

--------------------------------------------------
Free Database Connection Pooling Software
 
easy...your last code is essentially what i wrote on my second post.
 
As I said in that post, I found a solution, post it for the interest of someone else and perhaps somebody had something else to add or a better approach...
 
Oh, I thought you were asking out to use that syntax in your second post.
Well, at least there is a full description of how to use reflection in this post now !

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top