title: A list of objects in Java
body: <p>I'm trying to make a list containing different objects.</p>
  
  <pre><code>List&lt;Object&gt; list = new ArrayList&lt;Object&gt;();
      defObject defObj;
      optObject optObj;
  </code></pre>
  
  <p>and defObject has just one property for a String.</p>
  
  <pre><code>    public static class defObject
  {
      public static String defObj;
  
      public defObject(String x)
      {
          setDefObj(x);           
      }
  
      public static String getDefObj() {
          return defObj;
      }
  
      public static void setDefObj(String defObj) {
          defObject.defObj = defObj;
      }           
  }
  </code></pre>
  
  <p>if I add multiple defObjects to the list and go through the list after I'm done adding the element they all contain the same string, which was of the last defObject added to the list.</p>
  
  <p>I'm doing something like this to add the objects to the list:</p>
  
  <pre><code>   if (whatever)
         list.add(defObj = new defObject("x"));
      else if(whatever)
         list.add(defObj = new defObject("y"));
  </code></pre>
  
  <p>and the result is two defObjects with a string of "y"</p>
  
  <p>Please help me figure out why the objects aren't being added correctly and the properties are all same as the last defObj added to the list.</p>