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-15-2008, 09:46 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Dynamic method calling
The example presented below shows how to call a method dynamically:

Code:
public class TestReflect { public static void main(String s[]) throws Exception{ String aClass; String aMethod; // we assume that called methods have no argument Class params[] = {}; Object paramsObj[] = {}; while (true) { aClass = lineInput("\nClass : "); aMethod = lineInput("Method: "); // get the Class Class thisClass = Class.forName(aClass); // get an instance Object iClass = thisClass.newInstance(); // get the method Method thisMethod = thisClass.getDeclaredMethod(aMethod, params); // call the method System.out.println (thisMethod.invoke(iClass, paramsObj).toString()); } /* Output examples: Class : Class1 Method: class1Method2 ### Class 1, Method2 ### Class : java.util.Date Method: toString Sat Aug 11 13:18:39 EDT 2007 Class : java.util.Date Method: getTime 1186852732140 */ } public static String lineInput (String prompt) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print(prompt); return input.readLine(); } } class Class1 { public String class1Method1() { return "*** Class 1, Method1 ***"; } public String class1Method2() { return "### Class 1, Method2 ###"; } }
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
method calling? frejon26 New To Java 4 01-25-2008 04:38 AM
Calling method from another class asahli New To Java 1 12-15-2007 07:24 PM
i'm no good in calling Method please help lowpro New To Java 1 11-26-2007 06:15 PM
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 09:33 PM.


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