Results 1 to 9 of 9
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default how to use java.reflection with parameter(s)

    how to use java.reflection with parameter(s)

    Java Code:
    public class MyClass1{
        Class1 cls1 = new Class1();
        
        public void myMethod(String s) {
            cls1.msgBox(null, s, Class1.MESSAGE_INFO);
        }
    }
    
    //=============
    
    String fcn = "learn38.MyClass1";
    Class c1 = Class.forName(fcn);
    Method meth1 = c1.getMethod("myMethod", "hello world");
    Object theObject1 = c1.newInstance();
    meth1.invoke(theObject1);

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Maybe by, I don't know, reading the API for Method.invoke()?

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Just want to share..

    String fcn = "learn38.MyClass1";
    Class c1 = Class.forName(fcn);
    Object theObject1 = c1.newInstance();
    Method meth1 = c1.getDeclaredMethod("myMethod", new Class[]{String.class});

    String argu = "This is String to pass";
    meth1.invoke(theObject1, new Object[]{argu});

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Don't actually need the array (unless you are using an earlier version of Java of course).
    Just list the parameters.

  5. #5
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    hi.. thanks .. but could you please more detail?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    This is the method:
    invoke(Object obj, Object... args)

    The "..." means you can pass in a list of arguments, eg:
    Java Code:
    method.invoke(object, param1, param2, param3);
    or, in your case:
    Java Code:
    meth1.invoke(theObject1, argu);

  7. #7
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    so you meant:


    Default
    Just want to share..

    String fcn = "learn38.MyClass1";
    Class c1 = Class.forName(fcn);
    Object theObject1 = c1.newInstance();
    Method meth1 = c1.getDeclaredMethod("myMethod", String.class, String.class);

    String argu1 = "This is String 1 to pass";
    String argu2 = "This is String 2 to pass";
    meth1.invoke(theObject1, argu1, argu2);
    Last edited by newbiejava; 08-25-2010 at 09:21 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Yes.
    And that getDeclaredMethod Strings should be String.class (as you had in the original one), but the compiler would catch that for you.

    I suspect (though I haven't looked) that the compiler wraps those up in an array in any case, but it means you don't have to clutter up your code with extra "new Object[]{}" fluff.

  9. #9
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2010, 10:08 AM
  2. java.sql.SQLException: Missing IN or OUT parameter at index:: 1
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-08-2010, 08:45 PM
  3. Java reflection: find serializable classes in package
    By andrew222 in forum Advanced Java
    Replies: 2
    Last Post: 02-27-2010, 07:36 AM
  4. Replies: 1
    Last Post: 11-12-2009, 08:53 PM
  5. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

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