The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
In this section, we discuss the use of the 在本节中,我们将讨论如何使用static
keyword to create fields and methods that belong to the class, rather than to an instance of the class.static
关键字来创建属于类的字段和方法,而不是属于类的实例。
When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.当从同一个类蓝图创建多个对象时,每个对象都有各自不同的实例变量副本。In the case of the 对于Bicycle
class, the instance variables are cadence
, gear
, and speed
.Bicycle
类,实例变量是cadence
、gear
和speed
。Each 每个Bicycle
object has its own values for these variables, stored in different memory locations.Bicycle
对象都有自己的变量值,存储在不同的内存位置。
Sometimes, you want to have variables that are common to all objects.有时,您希望拥有对所有对象通用的变量。This is accomplished with the 这是通过static
modifier.static
修饰符完成的。Fields that have the 声明中包含static
modifier in their declaration are called static fields or class variables.static
修饰符的字段称为静态字段或类变量。They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.它们与类相关联,而不是与任何对象相关联。类的每个实例都共享一个类变量,该变量位于内存中的一个固定位置。Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.任何对象都可以更改类变量的值,但也可以在不创建类实例的情况下操作类变量。
For example, suppose you want to create a number of 例如,假设要创建多个Bicycle
objects and assign each a serial number, beginning with 1 for the first object.Bicycle
对象,并为每个对象指定一个序列号,第一个对象从1开始。This ID number is unique to each object and is therefore an instance variable.此ID号对于每个对象都是唯一的,因此是一个实例变量。At the same time, you need a field to keep track of how many 同时,您需要一个字段来跟踪已创建的Bicycle
objects have been created so that you know what ID to assign to the next one.Bicycle
对象的数量,以便知道要为下一个对象指定的ID。Such a field is not related to any individual object, but to the class as a whole.这样的字段与任何单个对象无关,而是与类作为一个整体相关。For this you need a class variable, 为此,您需要一个类变量numberOfBicycles
, as follows:numberOfBicycles
,如下所示:
public class Bicycle { private int cadence; private int gear; private int speed; // add an instance variable for the object ID private int id; // add a class variable for the // number of Bicycle objects instantiated private static int numberOfBicycles = 0; ... }
Class variables are referenced by the class name itself, as in类变量由类名本身引用,如中所示
Bicycle.numberOfBicycles
This makes it clear that they are class variables.这表明它们是类变量。
myBike.numberOfBicycles
You can use the 您可以使用Bicycle
constructor to set the id
instance variable and increment the numberOfBicycles
class variable:Bicycle
构造函数设置id
实例变量并递增numberOfBicycles
类变量:
public class Bicycle { private int cadence; private int gear; private int speed; private int id; private static int numberOfBicycles = 0; public Bicycle(int startCadence, int startSpeed, int startGear){ gear = startGear; cadence = startCadence; speed = startSpeed; // increment number of Bicycles // and assign ID number id = ++numberOfBicycles; } // new method to return the ID instance variable public int getID() { return id; } ... }
The Java programming language supports static methods as well as static variables.Java编程语言支持静态方法和静态变量。Static methods, which have the 静态方法的声明中包含static
modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as instatic
修饰符,应该使用类名调用,而无需创建类的实例,如中所示
ClassName.methodName(args)
instanceName.methodName(args)
A common use for static methods is to access static fields.静态方法的一个常见用途是访问静态字段。For example, we could add a static method to the 例如,我们可以向Bicycle
class to access the numberOfBicycles
static field:Bicycle
类添加一个静态方法来访问numberOfBicycles
静态字段:
public static int getNumberOfBicycles() { return numberOfBicycles; }
Not all combinations of instance and class variables and methods are allowed:并非所有实例和类变量及方法的组合都是允许的:
this
keyword as there is no instance for this
to refer to.this
关键字,因为没有可引用的实例。The static
modifier, in combination with the final
modifier, is also used to define constants.static
修饰符与finally
修饰符一起还用于定义常量。The final
modifier indicates that the value of this field cannot change.final
修饰符指示此字段的值不能更改。
For example, the following variable declaration defines a constant named 例如,以下变量声明定义了一个名为PI
, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter):PI
的常量,其值是π(圆的周长与其直径之比)的近似值:
static final double PI = 3.141592653589793;
Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so.以这种方式定义的常量不能被重新分配,如果您的程序试图这样做,这将是一个编译时错误。By convention, the names of constant values are spelled in uppercase letters.按照惯例,常量值的名称用大写字母拼写。If the name is composed of more than one word, the words are separated by an underscore (_).如果名称由一个以上的单词组成,则这些单词之间用下划线(_
)分隔。
Bicycle
ClassBicycle
类After all the modifications made in this section, the 在本节中进行所有修改后,Bicycle
class is now:Bicycle
类现在为:
public class Bicycle { private int cadence; private int gear; private int speed; private int id; private static int numberOfBicycles = 0; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; id = ++numberOfBicycles; } public int getID() { return id; } public static int getNumberOfBicycles() { return numberOfBicycles; } public int getCadence() { return cadence; } public void setCadence(int newValue) { cadence = newValue; } public int getGear(){ return gear; } public void setGear(int newValue) { gear = newValue; } public int getSpeed() { return speed; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }