title: How to add an object into ArrayList in java
body: <p>Here are two classes, query sentence and stackov. An arraylist is used to store objects of query sentence class.But the recently added object is overriding the previous one.How do I add objects so that they are not overridden? </p>
  
  <p><strong>QuerySentence.java</strong>   </p>
  
  <pre><code>public class QuerySentence {
  
      public static String query;
      public static String label;
      public QuerySentence(){
  
      }
      public QuerySentence(String query,String label){
          this.query = query;
          this.label = label;
      }
  }  
  </code></pre>
  
  <p><strong>Stackov.java</strong></p>
  
  <pre><code>package QueryClassifier;
  
  import java.util.ArrayList;
  
  public class stackov {
  
      public static void main(String args[])
      {
          QuerySentence qs1 = new QuerySentence("What state produces the best lobster to eat","LOCATION");
          QuerySentence qs2 = new QuerySentence("What is Dick Clark's birthday","DATE");
          ArrayList&lt;Object&gt; doclist = new ArrayList&lt;Object&gt;();
  
              doclist.add(0,qs1);
              doclist.add(1,qs2);
  
              int size = doclist.size();
                 while(size&gt;0)
                 {
                      QuerySentence qs3 = (QuerySentence) doclist.get(size-1);
                      System.out.println("\nin loop : " + qs3.label + qs3.query);
                      size--;
                  }
  
      }
  }   
  </code></pre>