title: How do I initialize classes with lots of fields in an elegant way?
body: <p>In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way?</p>
  
  <p>My current initialization step looks something like this:</p>
  
  <pre><code>public void testRequest() {
  
          //All these below used classes are generated classes from xsd schema file.
  
          CheckRequest checkRequest = new CheckRequest();
  
          Offers offers = new Offers();
          Offer offer = new Offer();
          HotelOnly hotelOnly = new HotelOnly();
          Hotel hotel = new Hotel();
          Hotels hotels = new Hotels();
          Touroperator touroperator = new Touroperator();
          Provider provider = new Provider();
          Rooms rooms = new Rooms();
          Room room = new Room();
          PersonAssignments personAssignments = new PersonAssignments();
          PersonAssignment personAssignment = new PersonAssignment(); 
          Persons persons = new Persons();
          Person person = new Person();
          Amounts amounts = new Amounts();
  
          offers.getOffer().add(offer);
          offer.setHotelOnly(hotelOnly);
  
          room.setRoomCode("roomcode");
          rooms.getRoom().add(room);
  
          hotels.getHotel().add(hotel);
          hotel.setRooms(rooms);
  
          hotelOnly.setHotels(hotels);
  
          checkRequest.setOffers(offers);
  
          // ...and so on and so on
      } 
  </code></pre>
  
  <p>I really want to avoid writing code like this, because it's a little messy having to instantiate each object separately and then initialize each field across multiple lines of code (e.g. having to call <code>new Offer()</code> and then <code>setHotelOnly(hotelOnly)</code> and then <code>add(offer)</code>).</p>
  
  <p>What elegant methods can I use instead of what I have? Are there any "<code>Factories</code>" that can be used? Do you have any references/examples to avoid writing code like this?</p>
  
  <p>I'm really interested in implementing clean code.</p>
  
  <hr>
  
  <p>Context:</p>
  
  <p>I'm developing a <code>RestClient</code> Application for sending post requests to a Webservice.</p>
  
  <p>The API is represented as a <code>xsd schema</code> file and I created all the Objects with <code>JAXB</code></p>
  
  <p>Before sending a request I have to instantiate many Objects because they have dependencies with each other. 
  <em>(An Offer has Hotels, a Hotel has Rooms, a Room has Persons... And these Classes are the generated ones)</em></p>
  
  <p>Thanks for your help.</p>