Instantiating an array using reflection

Use the java.lang.reflect.Array class.

Main.java:

import java.lang.reflect.*;
 
public class Main {
   public static void main(String args[]) throws Exception {
      // instantiation of an array of primitives
      int primitives[] = (int[]) Array.newInstance(int.class, 10);
 
      System.out.println(primitives);
 
      // instantiation of an array of objects
      Main objects[] = (Main[]) Array.newInstance(Main.class, 10);
 
      System.out.println(objects);
   }
}