Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:09:31+00:00 2026-06-15T20:09:31+00:00

The objective is simple, I want to create a method which load a class

  • 0

The objective is simple, I want to create a method which load a class dynamically, access its method and passing their parameters value and getting the return value at run-time.

Class which will be called

class MyClass {

    public String sayHello() {

        return "Hello";
    }

    public String sayGoodbye() {

        return "Goodbye";
    }

    public String saySomething(String word){
        return word;
    }
}

Main Class

public class Main {


    public void loadClass() {
        try {

            Class myclass = Class.forName(getClassName());

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().startsWith("saySome")) {
                    String word = "hello world";

                    //**TODO CALL OBJECT METHOD AND PASS ITS PARAMETER**
                } else if (methods[i].getName().startsWith("say")) {

                    //call method
                    System.out.println(methods[i].invoke(object));
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private String getClassName() {

        //Do appropriate stuff here to find out the classname

        return "com.main.MyClass";
    }

    public static void main(String[] args) throws Exception {

        new Main().loadClass();
    }
}

My question is how to invoke method with parameters and passing its value? also getting the return value and its type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:09:32+00:00Added an answer on June 15, 2026 at 8:09 pm

    I think you’re just missing the fact that you can pass in arguments to invoke, as an Object[]:

    Object result = methods[i].invoke(object, new Object[] { word });
    

    Or using varargs, if you prefer:

    Object result = methods[i].invoke(object, word);
    

    (The above two calls are equivalent.)

    See the documentation for Method.invoke for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, I want to create a simple video player which can play some
I'm making a simple program in Objective-C. It has one class with a lot
I am looking for a simple but efficient Objective-C algorithm which, starting from a
I am getting an unexpected value with a simple math calculation in Objective C.
Is it possible to create a simple objective C object in xcode that has
I want to create native Mac OS X application using Cocoa + Objective C
I am a new-be in Objective C. Here I want to ask a simple
I am creating simple web-service client in Objective-C and found that creating separate class
Simple question, just for fun . I want to create a category or something
I want to set the delegate of an object inside a class method in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.