Solved

Arrays of methods

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

12 Experts available now in Live!

Get 1:1 Help Now