fork download
  1. import java.lang.reflect.Array;
  2.  
  3. class Test {
  4. public int x;
  5.  
  6. public Test() {
  7. System.out.println("Test c-tor get called");
  8. }
  9. }
  10.  
  11. class ArrayTools {
  12. @SuppressWarnings("unchecked")
  13. public static <T> T[] initializedArray(int size, Class<T> classHint) throws java.lang.Exception {
  14. T[] result = (T[]) Array.newInstance(classHint, size);
  15. for (int i = 0; i < size; ++i) {
  16. result[i] = classHint.newInstance();
  17. }
  18. return result;
  19. }
  20. }
  21.  
  22. public class Main {
  23. public static void main(String[] args) throws java.lang.Exception {
  24. Test[] t = ArrayTools.initializedArray(3, Test.class);
  25. }
  26. }
  27.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Test c-tor get called
Test c-tor get called
Test c-tor get called