I need an explanation of when to use class variables?
<p>Suppose we have a class like:</p>

<pre><code>public class test { 
  int a=1;
  static int b=1;

  public int getA()
  {
    return a;
  }

  public void incrementA()
  {
    a++;
  }

  public void incrementB()
  {
    b++;
  }

  public int getB()
  {
    return b;
  }
}
</code></pre>

<p>and I have a class with main method like this</p>

<pre><code>public class testmain {
  /**
  * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    test t= new test();
    t.incrementA();

    test t1= new test();

    test t2= new test();
    t2.incrementB();    

    test t3 = new test();

    test t4= new test();
    t4.incrementB();

    System.out.println("t= "+t.getA());
    System.out.println("t1= "+t1.getA());
    System.out.println("t4= "+t4.getB());
    System.out.println("t3= "+t3.getB());
    System.out.println("t2= "+t2.getB());       
  }
}
</code></pre>

<p>I need the explanation about why <code>t</code> and <code>t1</code> have different value of <code>a</code>, and all <code>t2</code>, <code>t3</code>, <code>t4</code> have the same value of <code>b</code>. I know that I have declared <code>b</code> as static and all objects access the same address of that variable <code>b</code>. Why it is not causing any problem for a variable when each object it has its own <code>a</code>, now my question is since all the objects look to the same location in memory then why is <code>a</code> different value of each object?</p>
