separate23
separate23

Reputation:

In Java, why does untyped call of constructor of parameterised type provoke compiler warning?

In Java, why does an untyped invocation of a constructor of parameterised type provoke a compiler warning? Why is it okay to do similar thing with a static method? For example:

class Test<T> {

    Test() {}

    static <T> Test<T> create() {
        return new Test<T>();
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) {
        Test<String> warning = new Test();  // compiler warning - why?
        Test<String> okay = Test.create(); // no warning here - why?
        Test<String> okay2 = Test.<String>create(); // why doesn't it need this?
    }

}

Upvotes: 0

Views: 329

Answers (2)

Kolibri
Kolibri

Reputation: 567

Java does type inference on methods (which is why line 2 works and line 3 is not necessary), but not on constructors (which is why line 1 gives a warning).

It would be nice if Java did type inference on constructors too, but as of Java 6 it does not.

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545558

Because you assign an untyped instance to a typed variable. Your three cases:

  1. Because new Test<String>() would be correct.
  2. Because Java supports automatic type inference based on the return type, i.e. it can imply the missign generic argument here, and inside the method, you've made the generic type explict, by using new Test<T>() (instead of just new Test()).
  3. Because of 2. ;-)

Upvotes: 8

Related Questions