java的静态变量相当于类字段,而不用理解为对象字段.
static fields and methods 【程序编程相关:认识String之二:String与St】 【推荐阅读:C# 静态成员和方法的学习小结】in all sample programs that you have seen, the main method is tagged with the static modifier. we are now ready to discuss the meaning of this modifier. 【扩展信息:我在网上看到的,好站!】static fields
if you define a field as static, then there is only one such field per class. in contrast, each object has its own copy of all instance fields. for example, let´s suppose we want to assign a unique identification number to each employee. we add an instance field id and a static field nextid to the employee class:class employee
{ . . . private int id; private static int nextid = 1; }now, every employee object has its own id field, but there is only one nextid field that is shared among all instances of the class. let´s put it another way. if there are one thousand objects of the employee class, then there are one thousand instance fields id, one for each object. but there is a single static field nextid. even if there are no employee objects, the static field nextid is present. it belongs to the class, not to any individual object.
in most object-oriented programming languages, static fields are called class fields. the term "static" is a meaningless holdover from c++. let´s implement a simple method:
public void setid()
{ id = nextid; nextid++; }suppose you set the employee identification number for harry:
harry.setid();
then the id field of harry is set, and the value of the static field nextid is incremented:
... 下一页