Results 1 to 4 of 4
  1. #1
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default calling different methods based on a string?

    I don't think this is possible, but i thought i'd ask.

    if you have a string that is a method name like if a string was "findX" then with that you could call findX(...); using it, is this possible??

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    basically i'm writing a class where i could just say

    createThread("method');

    and a new thread would be spawned in method "method"

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    When you start thinking of stuff like that, it's usually time to redesign. Or, if the design really warrants it, use the Reflection API.

    db

  4. #4
    audriusa is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    This is very easy:

    Method m = getClass().getMethod("myMethod", new Class[0]);
    m.invoke(this, new Object[0]);

    I assume you want to invoke the method of the calling class with no parameters. Read about reflections for more, makes a lot of sense.

Similar Threads

  1. Calling methods into another class? Help please!
    By javanator in forum New To Java
    Replies: 1
    Last Post: 03-20-2010, 06:19 PM
  2. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  3. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •