Solved

Arrays of methods

Posted on 1997-05-26
5
233 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 82
Image decoding from Camera 3 72
Groovy:unable to resolve class error 2 69
Why my table column Id is not passed to java object? 4 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now