title: Casting List<T> to my own object
body: <p>I've got a method called <code>getBillingCodes</code> that returns a <code>List</code> of objects called <code>BillingCodes</code>.</p>
  
  <pre><code>@Service
  public class BillingCodeService {
  
      private static final Logger log = LoggerFactory.getLogger(BillingCodeService.class);
  
      String sql;
  
      @Autowired
      private BillingCodeRowMapper billingCodeRowMapper;
  
      @Autowired
      private JdbcTemplate jdbcTemplate;
  
      public List&lt;BillingCode&gt; getBillingCodes(int billingCodeId, int limit) {
  
          sql = [SQL_STATEMENT]";
  
      List&lt;BillingCode&gt; billingCodeList;
  
      billingCodeList = jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);
  
          return billingCodeList;
      }
  }
  </code></pre>
  
  <p>I would like to change the <code>getBillingCodes</code> method so that it still returns a <code>List</code> of objects but I like to specify the <code>List</code> of objects in a <code>class</code> like this</p>
  
  <pre><code>@Component
  public class BillingCodeList {
  
      private List&lt;BillingCode&gt; billingCodeList;
  
      Getter and Setters removed,,.       
  }
  </code></pre>
  
  <p>And then change the change the <code>getBillingCodes</code> method to:</p>
  
  <pre><code>@Service
  public class BillingCodeService {
  
      private static final Logger log = LoggerFactory.getLogger(BillingCodeService.class);
  
      String sql;
  
      @Autowired
      private BillingCodeRowMapper billingCodeRowMapper;
  
      @Autowired
      private JdbcTemplate jdbcTemplate;
  
      @Autowired
      private BillingCodeList billingCodeList;
  
      public BillingCodeList getBillingCodes(int billingCodeId, int limit) {
  
          sql = "[SQL_STATEMENT]";
  
          billingCodeList = jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);
  
          return billingCodeList;
      }
  }
  </code></pre>
  
  <p>Setting <code>billingCodeList = jdbcTemplate.query,,.</code> gives me an error <code>no instance(s) of variable(s) T exist so that List&lt;T&gt; conforms to BillingCodeList</code>. Therefore I've tried to cast the <code>List&lt;T&gt;</code> to <code>BillingCodeList</code> like this:</p>
  
  <pre><code>billingCodeList = (BillingCodeList)jdbcTemplate.query(sql, new Object[]{billingCodeId, limit}, billingCodeRowMapper);
  </code></pre>
  
  <p>But that gives me a casting error <code>java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.test.model.BillingCodeList</code>. How can I cast <code>java.util.ArrayList</code> to <code>com.test.model.BillingCodeList</code></p>