View Single Post
  #1 (permalink)  
Old 04-08-2008, 10:19 AM
ranu_gokhe ranu_gokhe is offline
Member
 
Join Date: Apr 2008
Posts: 1
ranu_gokhe is on a distinguished road
How to create object dynamically with class name known in string format
Hi,
I'm loading a class with following statement:

String stringToCompleteClassName = "someDynamicDigit";
Class classToLoad = Class.forName("com.somePackage.SomeotherPackage.so meClass" + stringToCompleteClassName );

Later i wd use reflection to get the methods of this class. now for invoking the methods with methodname.invoke() function i'd require the object of the loaded class. thus i want to create the object of the loaded class. I try to do it this way:

Object obj = classToLoad.newInstance();

but the problem in this is that this way i don't get the object of the class loaded but i get object of Object class.

Now if i want to call the functions of the loaded class, i do it like:
methodName.invoke(obj);

it throws an exception:
java.lang.IllegalArgumentException: object is not an instance of declaring class

can anybody please help?
Tell me if u need any clarification.

thanks in advance!
Reply With Quote
Sponsored Links