Java does not have an equivalent construct. There is no compile time safety on a class containing a constructor.
You can do it at runtime, but you have to either pass a non-null T or a Class as a parameter. The actual type parameter used is not retained at runtime.
public static <T> T[] createAndFillArray(T sampleObject, int size) throws Exception {
Class<T> klass = sampleObject.getClass();
T[] arr = (T[]) Array.newInstance(klass, size);
for (int i = 0; i < size; i++) {
arr[i] = klass.newInstance();
}
return arr;
}
The above will work but throw an exception if there is no public no argument constructor. You cannot get the compiler to enforce that there is one.
Edit: ChssPly76 beat me to it, so I modified the above code to give an example where you pass in an actual object sample, just to show how it is done. Normally in such a case you would pass in the class because the sampleObject doesn't end up in the array.