fork download
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class Main {
  6.  
  7. public interface Foo<T> {
  8. T getValue();
  9. void setValue(T value);
  10. }
  11.  
  12. public static class FooImpl<T> implements Foo<T> {
  13.  
  14. private T value;
  15.  
  16. public FooImpl(T value) {
  17. this.value = value;
  18. }
  19.  
  20. @Override
  21. public T getValue() {
  22. return value;
  23. }
  24.  
  25. @Override
  26. public void setValue(T value) {
  27. this.value = value;
  28. }
  29. }
  30.  
  31. public static <T> void resetFoos(Iterable<Foo<T>> foos) {
  32. for (Foo<T> foo : foos) {
  33. foo.setValue(foo.getValue());
  34. }
  35. }
  36.  
  37. public static void main(String[] args) {
  38.  
  39. final Foo<Object> objFoo = new FooImpl<>(new Object());
  40. final Foo<Integer> numFoo = new FooImpl<>(new Integer(42));
  41. final Foo<String> strFoo = new FooImpl<>("asdf");
  42.  
  43. List<Foo<?>> foos = new ArrayList<>(3);
  44. foos.add(objFoo);
  45. foos.add(numFoo);
  46. foos.add(strFoo);
  47.  
  48. resetFoos(foos);
  49.  
  50. System.out.println("done");
  51. }
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:48: error: method resetFoos in class Main cannot be applied to given types;
        resetFoos(foos);
        ^
  required: Iterable<Foo<T>>
  found: List<Foo<?>>
  reason: no instance(s) of type variable(s) T exist so that argument type List<Foo<?>> conforms to formal parameter type Iterable<Foo<T>>
  where T is a type-variable:
    T extends Object declared in method <T>resetFoos(Iterable<Foo<T>>)
1 error
stdout

Standard output is empty