1

Can anyone help me to find what's wrong with this piece of code run in jdk1.6.0_45?

public class App {
    public static void main(final String args[]) {
        SortComparator<String, Integer> a = build();
    }

    private static <T, O extends Comparable<O>> SortComparator<T, O> build() {
        return null;
    }

    public class SortComparator<T, O extends Comparable<O>> implement Comparator<T> {
        @Override
        public int compare(final T o1, final T o2) {
            return 1;
        }
    }
}

This is a pure Java language question. Why it does not compile? Why it does in Eclipse?

  • If I change SortComparator<String, Integer> to Map<String, Integer> it compiles.
  • If I add the parameter Class< O> arg as an argument of build it compiles.
  • If I add App. build() to the call it compiles.

Console output:

incompatible types; no instance(s) of type variable(s) T,O exist so that build<T,O> conforms to SortComparator<java.lang.String,java.lang.Integer>

Versions

  • Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
  • Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
  • Default locale: es_ES, platform encoding: Cp1252
  • OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Jordi P.S.
  • 3,838
  • 7
  • 36
  • 59
  • 2
    What error do you get from the compiler? – Jesper Dec 18 '13 at 16:11
  • The console output is: incompatible types; no instance(s) of type variable(s) T,O exist so that build conforms to SortComparator – Jordi P.S. Dec 18 '13 at 16:18
  • Note that Eclipse uses its own compiler, which might explain the difference in behaviour. – Oliver Charlesworth Dec 18 '13 at 16:19
  • possible duplicate of [Generics compiles and runs in Eclipse, but doesn't compile in javac](http://stackoverflow.com/questions/2858799/generics-compiles-and-runs-in-eclipse-but-doesnt-compile-in-javac) – Oliver Charlesworth Dec 18 '13 at 16:21
  • Your example is incomplete, this code compiles well in Java 1.6, I just added dummy implementation of Comparator#compare method in SortComparator – hoaz Dec 18 '13 at 16:26
  • @hoaz I have updated the code, I removed the parameter, now it fails (sorry for that) – Jordi P.S. Dec 18 '13 at 16:43

2 Answers2

0

Because the return type of SortComparator is not a tuple (or Map.Entry).

// Only takes one argument...
private <O extends Comparable<O>> SortComparator<O> build(final Class<O> clazz) {
  return null;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I tried following example and it compile fine in both Eclipse and from command line:

public class Test {

    public static class SortComparator<T, O extends Comparable<O>> implements Comparator<T> {
        @Override
        public int compare(T o1, T o2) {
            return 0;
        }
    }

    private static <T, O extends Comparable<O>> SortComparator<T, O> build(
            final Class<O> clazz) {
        // you can return null here too
        return new SortComparator<T, O>();
    }

    public static void main(String[] args) {
        SortComparator<String, Integer> a = build(Integer.class);
    }

}
hoaz
  • 9,883
  • 4
  • 42
  • 53
  • I think you have static import of `build` method in your class and it makes your App.build() invisible – hoaz Dec 18 '13 at 18:21