title: How to access a public static ArrayList from various classes?
body: <p>Suppose I have a class</p>
  
  <pre><code>    public class c1 {
          public static ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          public c1() {
              for (int i = 0; i &lt; 5; i++) {   //The size of the ArrayList is now 5
                  list.add("a");
              }
          }
      }
  </code></pre>
  
  <p>But if I access the same ArrayList in another class, I will get a list with SIZE = 0.</p>
  
  <pre><code>     public class c2 {
           public c2() {
               System.out.println("c1.list.size() = " + c1.list.size()); //Prints 0
           }
       }
  </code></pre>
  
  <p>Why is this happening. If the variable is static, then why is a new list being generated for class c2? How can I make sure that I get the same ArrayList if I access it in a different class?</p>
  
  <p>/<strong><em>*</em>**<em>*</em></strong><em>Revised code</em><strong><em>*</em>**<em>*</em>****</strong>/</p>
  
  <pre><code>     public class c1 {
          public static ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          public static void AddToList(String str) {       //This method is called to populate the list 
             list.add(str);
          }
      }
  </code></pre>
  
  <p>But if I access the same ArrayList in another class, I will get a list with SIZE = 0, irrespective of how many times I called AddToList method.</p>
  
  <pre><code>     public class c2 {
           public c2() {
               System.out.println("c1.list.size() = " + c1.list.size()); //Prints 0
           }
       }
  </code></pre>
  
  <p>How can I make sure that same changes appear when I use the ArrayList in another class?</p>