Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 10:19 AM
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2008, 04:15 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create, compile and call a Java source dynamically Java Tip Java Tips 0 02-17-2008 10:57 AM
Dynamically create a button, but what happened? love2java AWT / Swing 1 02-17-2008 02:01 AM
Object from String (calling method dynamically) Java Tip Java Tips 0 02-16-2008 11:22 PM
How to create widgets dynamically sarbuland Advanced Java 0 02-06-2008 10:08 PM


All times are GMT +3. The time now is 09:41 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org