Java 我需要解释什么时候使用类变量?

Java 我需要解释什么时候使用类变量?,java,Java,假设我们有这样一个类: 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; } } 我有一个类的主方法

假设我们有这样一个类:

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;
  }
}
我有一个类的主方法是这样的

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());       
  }
}
我需要解释为什么
t
t1
具有不同的
a
值,并且所有
t2
t3
t4
具有相同的
b
值。我知道我已将
b
声明为静态,并且所有对象都访问该变量
b
的相同地址。当每个对象都有自己的
a
时,为什么它不会对变量造成任何问题,现在我的问题是,既然所有对象都指向内存中的同一位置,那么为什么
a
每个对象的值不同呢?

类(静态)变量值将可访问该类的多个实例,在本例中是t…t4

当您使用t.b(或)t4.b(或)test.b(或)使用t.t4-->的任何方法更改“b”时,将更新相同的“b”值,因为在这些方法中只存在一个“b”副本

实例变量特定于该实例。”a'存在每个实例t…t4的副本


阅读本文了解更多详细信息。

每个对象都有自己的a示例。所以a和对象一样多

因为对于每个对象,a内存存储有两个位置,一个用于a,一个用于b,所有对象t…t4都具有相同的b值为什么a也不存在这种情况?我知道我已经把b设为static,但是这个static在变量的内存中做了什么呢?