0

I have n class (Class1, Class2, .., Classn), each have a static main methods.

I wish to store this class in an array, and call the main method once for each class. But what is the type that I have to declare for this array?

This is what I have done finally according to the reply from T.J. Crowder

ArrayList<Class> meoa = new ArrayList(
                    Arrays.asList(Class.forName("mypackage.Class1"),
                            Class.forName("mypackage.Class2"),
                            Class.forName("mypackage.Class3"),
                            Class.forName("mypackage.Class4"),
                            Class.forName("mypackage.Class5")));    


          for(Class cls:meoa) {
              System.out.println("Invoking:"+cls.getName());
              Method m = cls.getDeclaredMethod("main", String[].class);
              Object[] arg = new Object[1];                  
              arg[0] = new String[] {};
              m.invoke(null, arg);
          }
1
  • Rather than calling list of main method, you should chain the functionalists of those methods. Commented Mar 10, 2014 at 11:17

2 Answers 2

4

The type is Class[] (array of Class). (Since main is a static method, we can't really use an interface for this.)

When you're ready to call main, you'd do that via reflection, e.g.:

Class[] classes = /*...create and fill in the array...*/;
for (Class cls : classes) {
    // Look up the main method accepting an array of strings
    Method m = cls.getDeclaredMethod("main", String[].class);
    m.invoke(null, /*...see note below...*/);
}

Re "see note below" above: The argument you pass invoke is an Object[] (array of Object) containing the arguments to give main. Since main expects a single argument (an array of strings), you have to pass it a single-entry Object[] where that one entry is String[]. So for instance, if you were going to pass ["one", "two", "three"] to main, you'd construct the invoke argument like this:

Object[] arg = new Object[1];                    // Room for one argument
arg[0] = new String[] { "one", "two", "three" }; // That one arg is a String array
m.invoke(null, arg);                             // Invoke main

(The null in the call to invoke is for the instance on which to call the method; since main is static, we supply null for that.)

Sign up to request clarification or add additional context in comments.

3 Comments

@T.J. Crowder any reference?
@swapnil7: Reference for what?
@swapnil7: I linked to Class in the JDK reference (the word Class in the above is a link).
0

Arrays in Java (and most statically-typed languages) can only store one type at a time. If the classes all extend the same superclass, then you can declare an array that can store all those classes as follows:

SuperClass[] array = new Superclass[];

Alternatively, if you didn't actually mean (Class1, Class2, ..., Classn) but (object1, object2, ..., objectn), where each object is an instance of some class SomeClass, then that's just:

SomeClass[] array = new SomeClass[];

As for calling the main method on each, just loop through said array and call the method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.