Results 1 to 2 of 2
- 04-08-2008, 08:19 AM #1Member
- Join Date
- Apr 2008
- Rep Power
How to create object dynamically with class name known in string format
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:
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!
- 04-09-2008, 02:15 AM #2
After just attempting to play with this setup, I found that I needed to check for three Exceptions.... maybe a try/catch block will do?
By the way, Welcome to the Forums!
See you around! :)
- By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:57 AM
- By love2java in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 01:01 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 10:22 PM
- By sarbuland in forum Advanced JavaReplies: 0Last Post: 02-06-2008, 09:08 PM