title: Java method to assign object field values with Reflection
body: <p>I was wondering whether it would be possible to have something like the following in Java:</p>
  
  <pre><code>public class MyClass {
      private String name;
      private Integer age;
      private Date dateOfBirth;
      // constructors, getters, setters
  
      public void setField(String aFieldName, Object aValue) {
          Field aField = getClass().getDeclaredField(aFieldName);
          // use: aField.set(...) with proper type handling
      }
   }
  </code></pre>
  
  <p>I am really stuck in the setField method and any idea would be very helpful.</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: The reason for this is that I would like to have a method in another class like the following</p>
  
  <pre><code>public static MyClass setAll(List&lt;String&gt; fieldNames, List&lt;Object&gt; fieldValues) {
      MyClass anObject = new MyClass();
      // iterate fieldNames and fieldValues and set for each fieldName 
      // the corresponding field value
      return anObject;
  }
  </code></pre>