Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 02-16-2008, 10:22 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Object from String (calling method dynamically)
The example presented below, creates object from String by passing a parameter to the constructor and calling a method dynamically.

Code:
public class Test { public static void main(String args[]) { try { String name = "java.lang.String"; String methodName = "toLowerCase"; // get String Class Class cl = Class.forName(name); // get the constructor with one parameter java.lang.reflect.Constructor constructor = cl.getConstructor (new Class[] {String.class}); // create an instance Object invoker = constructor.newInstance (new Object[]{"REAL'S HOWTO"}); // the method has no argument Class arguments[] = new Class[] { }; // get the method java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments); // convert "REAL'S HOWTO" to "real's howto" Object result = objMethod.invoke (invoker, (Object[])arguments); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
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
How to create object dynamically with class name known in string format ranu_gokhe Advanced Java 1 04-09-2008 03:15 AM
Method to write if the string object can be converted to Integer or not. Abhishek New To Java 4 03-25-2008 01:16 PM
Dynamically calling a method with 2 argumens Java Tip Java Tips 0 02-15-2008 09:49 AM
method calling? frejon26 New To Java 4 01-25-2008 04:38 AM
Help with Calling a method Albert New To Java 3 07-10-2007 04:27 PM


All times are GMT +3. The time now is 07:03 PM.


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