One purpose of a generic constructor may be the same as for some common methods. To ensure that multiple arguments are of the same type.
Consider the following example (yes, this is a little far-fetched, but should show a point):
import java.util.ArrayList; import java.util.Collection; public class GenericConstructorTest { public static void main(String[] args) { Collection<String> strings = new ArrayList<String>(); ClassWithParam c0 = new <String>ClassWithParam("String", strings); } } class ClassWithParam { public <T> ClassWithParam(T data, Collection<T> collection) { collection.add(data); } }
It does not matter for the class or its constructor, which is exactly used there. It is only important to know that the collection specified as the second argument can accept elements of the type specified as the first argument.
(I would like to show a more realistic, practical example, but I think that itโs quite rare to parameterize the constructor in this way, and you might even think that this is just a โside effectโ of the ability to parameterize methods and the fact that there is no reason to explicitly forbid this for designers ...)
EDIT As requested in a comment and an example where these arguments are commonly used. This is still far-fetched. More realistic examples may be structurally similar and apply to some types of listeners, but will include much more code:
public class GenericConstructorTest { public static void main(String[] args) { Callback<String> callback = new Callback<String>(); ClassWithParam c0 = new <String>ClassWithParam("String", callback); c0.execute(); } } class ClassWithParam { private Runnable runnable; public <T> ClassWithParam(final T data, final Callback<T> callback) { runnable = new Runnable() { @Override public void run() { callback.call(data); } }; } void execute() { runnable.run(); } } class Callback<T> { void call(T t) { System.out.println("Called with "+t); } }
The second example you posted
Integer val = new <String>Integer(100);
does not compile in Eclipse with Java 7. It complains
An Integer (int) constructor of type Integer is not generic; it cannot be parameterized with arguments
In Java 8, this is allowed, although it still throws a warning:
Unused type arguments for a non-generic Integer (int) constructor of type Integer; it cannot be parameterized with arguments
(If this difference interests you, you can consider the issue as a separate issue)