In Java (and in OOP in general) the objects have two kinds of fields(variable).
Instance variables(or object variable) are fields that belong to a particular instance of an object.
Static variables (or class variable) are common to all the instances of the same class.
Here's an example:
public class Foobar{
static int counter = 0 ; //static variable..all instances of Foobar will share the same counter and will change if such is done
public int id; //instance variable. Each instance has its own id
public Foobar(){
this.id = counter++;
}
}
usage:
Foobar obj1 = new Foobar();
Foobar obj2 = new Foobar();
System.out.println("obj1 id : " + obj1.id + " obj2.id "+ obj2.id + " id count " + Foobar.counter);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…