Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Arrays of methods

Posted on 1997-05-26
5
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 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