0

Below code throws

"Incompatible types. Required Sample<Void> but create was inferred to Sample<T>. Incompatible equality constraints:Void and Object". 

public class SampleClass{
  public static <T> Sample<T> create(String str) {}
}

Sample<Void> sample = SampleClass.create("abc");
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
dj308
  • 47
  • 3
  • 6
  • 1
    What's `Sample`? Please provide a [mcve], [cannot reproduce](https://ideone.com/T596o0). – Andy Turner Apr 06 '20 at 16:02
  • Check this: https://stackoverflow.com/questions/3012781/java-syntax-for-explicitly-specifying-generic-arguments-in-method-calls/3012789 – Buddhima Gamlath Apr 06 '20 at 16:05
  • Does this answer your question? [Java-syntax for explicitly specifying generic arguments in method calls](https://stackoverflow.com/questions/3012781/java-syntax-for-explicitly-specifying-generic-arguments-in-method-calls) – Buddhima Gamlath Apr 06 '20 at 16:10
  • pls ignore this question.. it was something wrong on my side. Sorry – dj308 Apr 13 '20 at 22:37

1 Answers1

0

It actually does work.

Full example:

public class SampleClass {

    public static void main(String[] args) {
        Sample<Void> sample = SampleClass.create("abc");

    }

    public static <T> Sample<T> create(String str) {
        return null;
    }

    private static class Sample<T> {
    }
}

Compiles and executes just fine (with JDK 11).

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348