4

In C#, I can put a type constraint on a generic parameter that requires the generic type to have a default parameterless constructor. Can I do the same in Java?

In C#:

    public static T SomeMethodThatDoesSomeStuff<T>() where T : class, new()
    {
            // ... method body ...
    }

The class and new() constraints mean that T must be a class that can be called with the new operator with zero parameters. What little I know of Java generics, I can use extends to name a required super class. Can I use that (or any other supported operation) to achieve the above functionality?

1

3 Answers 3

9

No; in Java the typical solution is to pass a class, and doc the requirement of 0-arg constructor. This is certainly less static checking, but not too huge a problem

/** clazz must have a public default constructor */
public static <T> T f(Class<T> clazz)
    return clazz.newInstance();
Sign up to request clarification or add additional context in comments.

Comments

3

No.

Because of type erasure, the type of <T> is not known at runtime, so it's inherently impossible to instantiate it.

4 Comments

Well somewhat. The problem is that contrary to C# in Java we can't specify constraints on constructors - ie specifying what kind of constructor an interface has to offer. Even with type erasure if we could do this, the above would work just fine and it would be oh so useful. Sadly it doesn't work, which means we have to use reflection to instantiate types and have to specify somewhere in the API "Has to offer argument less public constructor" - bad design flaw that.
The type erasure is not exactly a problem. The class is known at compile time so at least such a constraint, if it was present in JLS, could be enforced easily by the compiler
@usr-local-ΕΨΗΕΛΩΝ: Yes, but it wouldn't be very useful.
Personally, but that's just my opinion, it would be the opposite. Yesterday we did have such a problem and with a similar constraint we could easier find it out before running the web application
0

Java does not do structural typing, so you cannot constrain T to have a particular constructor or static method. Only subtyping and supertyping restrictions are permitted.

So, pass an appropriate abstract factory. Use of reflection here, or in virtually any case, is highly inappropriate. There is now a now a suitable generic JDK type for this in the form of java.util.function.Supplier.

public static T someMethodThatDoesSomeStuff<T>(
    Supplier<T> factory
) {

Invoke as:

Donkey donkey = someMethodThatDoesSomeStuff(BigDonkey::new);

(The method reference needn't be a constructor.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.