Syntax and Purpose of Java Syntax Constructors

Java constructors can be shared: http://docs.oracle.com/javase/tutorial/java/generics/methods.html

However, I cannot find a good example of how to declare and call a common constructor. Moreover, I do not understand the purpose of the universal constructor, since the scope of type parameters is limited by the constructor.

The presence of a class with a common constructor:

public class MyClass { public <T> MyClass(T data) { // ... } } 

we call it like:

 MyClass obj = new <Integer>MyClass(12); 

So my questions are:

  • What is the purpose of a universal constructor? Can you show an example from the JDK or your own example?

  • Why is the operator like

     Integer val = new <String>Integer(100); 

compiled without errors, even if the Integer class does not have a common constructor?

+6
source share
1 answer

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)

+6
source

Source: https://habr.com/ru/post/976252/


All Articles