Can I create a generic constructor without creating a class?

I saw in Java that you can create a generic class and a generic method. I also saw the codes that the constructor makes with the class. Is it possible to create only a constructor? And if so, how to call the constructor?

+6
source share
1 answer

Yes, you can.

class Example { public <T> Example(T t) {} public static void main(String[] args){ // In this example the type can be inferred, so new Example("foo") // works, but here is the syntax just to show you the general case. Example example = new<String>Example("foo"); } } 
+4
source

All Articles