Want to create object on the fly
I want to create object of any defined type.
Suppose I have got to create object of class "MyClass" and constructor values like int, string.
This creation has to be dynamic in nature , meant , the class or constructor params can be had with any changing values
Example : I call a method where I am passing the class name and param lists of constructor. Now I want to create the object of that class using the constructor passing the param lists.
Thanks
# 1
You are posting in the right forum, but you forgot to pose any questions.
# 2
Question is : how to create the object in above situation when you have got the Class name and Constructor arg lists on runtime
# 3
Class<?> c = Class.forName(name);Constructor<?> ctor = Class.getConstructor(argTypes);Object object = ctor.newInstance(args);
ejpa at 2007-7-12 21:22:35 >

# 4
But this generates a compilation error saying "Class is not generic;it can not be parameterized with arguments <?>
# 5
Sorry,Constructor<?> ctor = c.getConstructor(argTypes);anyway you get the idea, surely you can fix the compilation errors yourself?
ejpa at 2007-7-12 21:22:35 >

# 6
What you are saying is probably not applicable here in my case.I tried with what you are saying but still getting same compilation error.What to do next?Can you provide any solution ?Thanks
# 7
The code I provided compiles for me as amended. Are you using Eclipse by any chance? in which case you need to upgrade it.
>What you are saying is probably not applicable here in my case.
What you are saying here is that you aren't competent to fix one compile error in four lines of code.
You could try removing all the generics stuff for a start.
> Can you provide any solution ?
I already did that. Do you mean another solution?
ejpa at 2007-7-12 21:22:35 >

# 8
> What you are saying is probably not applicable here
> in my case.
> I tried with what you are saying but still getting
> same compilation error.
You are obviously compiling to a JDK before 5. You should either change the JDK or strip the generics from the example. Of course, you have to replace the placeholders ejp used.
> What to do next?
> Can you provide any solution ?
If you cannot adapt the example, you actually should step back and start learning about reflection in Java before trying to apply it. There is a tutorial on reflection:
http://java.sun.com/docs/books/tutorial/reflect/index.html
# 9
> You are obviously compiling to a JDK before 5.I don't think a JDK before 1.5 would give that error message. Maybe he has some strange mixture of the JDK 1.5 compiler and the 1.4 rt.jar?
ejpa at 2007-7-12 21:22:36 >

# 10
> > You are obviously compiling to a JDK before 5.
>
> I don't think a JDK before 1.5 would give that error
> message. Maybe he has some strange mixture of the JDK
> 1.5 compiler and the 1.4 rt.jar?
It says that "Class is not generic". So I am very sure that he uses a pre-5 JDK in his classpath and compiles using javac 1.5 or greater.
Ok, my remark may not have been that clear. I said "compiling to" not "compiling using" :)
# 11
I got the things working but with a different solution(without generics).
Thanks for your help which gives me good essence of what a forum should not be.
One should remember that a forum should be utilized to help others so that the helper can practice and enhance his/her knowledge.
A forum should not be a place to sound own's bit.
Thanks
# 12
> I got the things working but with a different
> solution(without generics).
>
> Thanks for your help which gives me good essence of
> what a forum should not be.
>
> One should remember that a forum should be utilized
> to help others so that the helper can practice and
> enhance his/her knowledge.
>
> A forum should not be a place to sound own's bit.
What?
# 13
> I got the things working but with a different
> solution(without generics).
That's exactly what you were advised to do here, and you were also advised here that your solution must involve Class.forName, Class.getConstructor, and Constructor.newInstance.
If you haven't done all that, exactly as advised here, you don't yet have the solution to your problem as you described it.
On these forums nobody worth listening to will write your code for you, and they won't help you fix trivial compile errors either. That's what you're paid for. On the other hand, the advice you get here, after you filter out the garbage, is as good as it gets.
ejpa at 2007-7-12 21:22:36 >
