在Java(通常在OOP中)对象有两种字段(变量).
实例变量(或对象变量)是属于对象的特定实例的字段.
静态变量(或类变量)对同一个类的所有实例都是常见的.
这里有一个例子:
public class Foobar{
static int counter = 0 ; //static variable..all instances of Foobar will share the same counter
public int id; //instance variable. Each instance has its own id
public Foobar(){
this.id = counter++;
}
}
用法:
Foobar obj1 = new Foobar();
Foobar obj2 = new Foobar();
System.out.println("obj1 id : " + obj1.id + " obj2.id "= obj2.id + " id count " + Foobar.counter);