title: Java: Getting the properties of a class to construct a string representation
body: <p>Let's say I have a class like this (and also further assume that all the private variables:</p>
  
  <pre><code>public class Item {
      private String _id = null;
      private String _name = null;
      private String _description = null;
  
          ...
  }
  </code></pre>
  
  <p>Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class:</p>
  
  <pre><code>@Override
  public String toString() {
      return (_id + " " + _name + " " + _description);
  }
  </code></pre>
  
  <p>But what if I have say 15 private variables inside the class? Do I have to write the name of each and every variable like this? </p>
  
  <p>Ideally, I would like to get over with the task by iterating through the list of private variables of this class and construct the string representation:</p>
  
  <pre><code>@Override
  public String toString() {
      ArrayList&lt;String&gt; members = getClass().getMembers(); //Some method like this
      String string = "";
      for(...)
          string += members[i] + " ";
  }
  </code></pre>
  
  <p>Or perhaps a toJSON method, I would still need access to the names of these variables. Any suggestions?</p>