title: Java - Best way to return multiple object types from a method
body: <p>In my DAO i have a method where i build 2 different objects and I want to return both of those objects, but i'm not sure what the best way is to do it. I've looked at using <code>? extends myObject</code>, creating another class that holds both of my objects that i want to return, and just using <code>List&lt;Object&gt;</code>.</p>
  
  <p>Long story short on why i need these similar objects is to display 1 on the screen and the other to use with primefaces dataexporter which doesn't handle lists in an object as far as i'm aware.</p>
  
  <p>Class Person</p>
  
  <pre><code>public class Person() {
    firstName = null;
    lastName = null;
    List&lt;Programs&gt; programs = new ArrayList&lt;Programs&gt;();
  
    // Getters and setters
  }
  </code></pre>
  
  <p>Class DataExporterPerson</p>
  
  <pre><code>public class DataExporterPerson() {
    firstName = null;
    lastName = null;
    String program = null;
  
    // Getters and setters
  }
  </code></pre>
  
  <p>DAO method:</p>
  
  <pre><code>public List&lt;SOMETHING&gt; getPeople() {
    // query db for people
  
    // build both objects
  
    return ?????
  }
  </code></pre>
  
  <p>Now i understand i can very easily create another object like the one below, but that seems like an inefficient way to do things because i'm basically creating an object just to return from 1 method. </p>
  
  <pre><code>public class PersonTransporter() {
    Person person = null;
    DataExporterPerson = null;
  }
  </code></pre>
  
  <p>What is the best way to handle this scenario?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The reason that i'm trying to return 2 objects in 1 method is because this is a DAO method that queries the database and builds 2 objects based on the data in the query. I don't want to break it up into 2 methods because i don't want to query the db twice if i don't need to.</p>