Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Arrays of methods

Posted on 1997-05-26
5
Medium Priority
?
282 Views
Last Modified: 2010-05-18
How would you implement an array of methods? I have tried getMethods() & invoke() but can't get that to work. Are there other ways of doing it?
0
Comment
Question by:pdrake
5 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220907
Both class and method are mannaged as pointer internaly, have you tried storing the value for this.method in an array of Objects?


0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220908
You can make an interface with a single method:

public interface MethodInterface {
   public myMethod(String[] args);
}

and then make an array of Objects that implement the method.

MethodInterface[] methodArray = new MethodInterface[10];

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220909
Take a look at:

http://www.panix.com/~rangerx/packages.html

it has a class library that will probably help a lot.

Let me know if it solves your problem.
0
 

Accepted Solution

by:
hosseinakhlaghpour earned 150 total points
ID: 1220910

You should instantiate the class and passed the parameters as an object[]
The following is some part of my code which works fine.

class2load = Class.forName(...);
classInstance = class2load.newInstance();

If the method does not get any parameter you can leave the params=null

      Object[] params = null;

      ......
      .......

      Method classMethod = null;
      Method[] classMethods = class2load.getMethods();

      for(int indx4classMethods = 0; indx4classMethods != classMethods.length; indx4classMethods++)
      {
            if(classMethods[indx4classMethods].getName().equals(methodName))
            {
                  classMethod = classMethods[indx4classMethods];
            }
      }

      Object returnObject = classMethod.invoke(classInstance, params);

0
 

Author Comment

by:pdrake
ID: 1220911
This seems a little messy. While I was waiting for an answer I figured out how to implement the array of methods I needed with an interface instead of .reflect and .Class. It's probably less powerful but perfectly satisfies my requirements. Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question