1 Reply Latest reply: Jan 22, 2012 4:24 PM by EJP RSS

    Beginner's question about generics

    912541
      Hello,

      While experimenting with generics, I stumbled onto the following puzzle. The following code does not compile and I am not sure I understand why. Everything seems type safe to me.
      import javax.swing.RowSorter.SortKey;
      import javax.swing.table.TableModel;
      import javax.swing.table.TableRowSorter;
      
      import java.util.List;
      
      public class Test {
          
          public static void main(String[] args) {
              TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>();
              List<? extends SortKey> keys = sorter.getSortKeys();
              SortKey key = new SortKey(0, null);
              keys.add(key);
              sorter.setSortKeys(keys);
          }
          
      }
      Can someone help me understand why this code is not valid and how to fix it?

      Regards,

      Tom