简体   繁体   中英

Incompatible types Void and Object - Java Generics

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");

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM