Solved

Arrays of methods

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 156
check java version using powershell 13 304
Fast way to search item into Java Array (Rhino compatible) 2 39
collection output issue 9 66
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

726 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