title: Java Reflection // Can not set java.lang.Boolean to (boolean)true
body: <p>I have a class which has a field named "bool" of type <em>java.lang.Boolean</em> and try to set its value via reflection with the following code:</p>
  
  <pre><code>MyClass myClass = new MyClass();
  try {
      Field field = myClass.getClass().getField("bool");
      field.setBoolean(myClass, true);
  } catch (Throwable ex) {
      ex.printStackTrace();
  }
  </code></pre>
  
  <p>I always get the following error: <em>java.lang.IllegalArgumentException: Can not set java.lang.Boolean field MyClass.bool to (boolean)true</em></p>
  
  <p>Passing an instance of <em>new Boolean</em> instead of <em>true</em>, <em>Boolean.TRUE</em>, or <em>new Boolean(true).booleanValue()</em> didn't work either.
  Interestingly, it works flawlessly if I change the type of <em>java.lang.Boolean</em> to <em>boolean</em>. However, I need to be able to process java.lang.Boolean, too.</p>
  
  <p>How can I set a java.lang.Boolean via reflection?</p>