Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first argument of the <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html#set(java.lang.Object,%20java.lang.Object)" rel="nofollow noreferrer"><code>Field.set</code></a> method should be the object which you are reflecting on.</p> <pre><code>decfields[0].set("msg", "success"); </code></pre> <p>Should read:</p> <pre><code>decfields[0].set(test, "success"); </code></pre> <p>Furthermore, the final <code>System.out.println</code> call should refer to the <code>test</code> object rather than the class <code>ColinTest</code>, as I presume the intention is to output the contents of the <code>test.msg</code> field.</p> <p><strong>Update</strong></p> <p>As pointed out by <a href="https://stackoverflow.com/questions/919962/illegalargumentexception-when-setting-public-member/919986#919986">toolkit</a> and <a href="https://stackoverflow.com/questions/919962/illegalargumentexception-when-setting-public-member/919985#919985">Chris</a>, the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaredField(java.lang.String)" rel="nofollow noreferrer"><code>Class.getDeclaredField</code></a> method can be used to specify the name of the field in order to retrieve it:</p> <pre><code>Field msgField = test.getClass().getDeclaredField("msg"); // or alternatively: Field msgField = ColinTest.class.getDeclaredField("msg"); </code></pre> <p>Then, the <code>set</code> method of the <code>msgField</code> can be invoked as:</p> <pre><code>msgField.set(test, "success"); </code></pre> <p>This way has its benefit, as already pointed out by toolkit, if there are more fields added to the object, the order of the fields that are returned by <code>Class.getDeclaredFields</code> may not necessarily return the field <code>msg</code> as the first element of the array. Depending on the order of the returned array to be a certain way may cause problems when changes are made to the class.</p> <p>Therefore, it would probably be a better idea to use <code>getDeclaredField</code> and declare the name of the desired field.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload