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发行说明。
As you learned in the previous lesson, an object stores its state in fields.如前一课所述,对象将其状态存储在字段中。
int cadence = 0; int speed = 0; int gear = 1;
The What Is an Object? discussion introduced you to fields, but you probably have still a few questions, such as: What are the rules and conventions for naming a field?什么是对象?的讨论向您介绍了字段,但您可能还有一些问题,例如:命名字段的规则和约定是什么?Besides 除了int
, what other data types are there? Do fields have to be initialized when they are declared?int
,还有哪些其他数据类型?声明字段时是否必须初始化字段?Are fields assigned a default value if they are not explicitly initialized?如果字段未显式初始化,是否为其指定了默认值?We'll explore the answers to such questions in this lesson, but before we do, there are a few technical distinctions you must first become aware of.在本课中,我们将探讨这些问题的答案,但在此之前,您必须首先了解一些技术区别。In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.在Java编程语言中,术语“字段”和“变量”都被使用;这在新开发人员中是一个常见的混淆源,因为两者似乎经常提到同一件事。
The Java programming language defines the following kinds of variables:Java编程语言定义了以下类型的变量:
static
keyword.static
关键字。currentSpeed
of one bicycle is independent from the currentSpeed
of another.static
modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.static
修饰符声明的任何字段;这会告诉编译器,不管类被实例化了多少次,该变量只有一个副本存在。static
since conceptually the same number of gears will apply to all instances.static
,因为概念上相同的档位数量将应用于所有实例。static int numGears = 6;
would create such a static field.static int numGears=6;
将创建这样一个静态字段。final
could be added to indicate that the number of gears will never change.final
,表示档位的数量永远不会改变。int count = 0;
).int count = 0;
)。Bicycle
class and in the main
method of the "Hello World!" application.Bicycle
类和“Hello World!”的main
方法中看到了参数示例应用。main
method is public static void main(String[] args)
.main
方法的签名是public static void main(String[] args)
。args
variable is the parameter to this method.args
变量是此方法的参数。Having said that, the remainder of this tutorial uses the following general guidelines when discussing fields and variables.话虽如此,本教程的其余部分在讨论字段和变量时使用以下一般准则。If we are talking about "fields in general" (excluding local variables and parameters), we may simply say "fields".如果我们谈论的是“一般字段”(不包括局部变量和参数),我们可以简单地说“字段”。If the discussion applies to "all of the above", we may simply say "variables".如果讨论适用于“上述所有”,我们可以简单地说“变量”。If the context calls for a distinction, we will use specific terms (static field, local variables, etc.) as appropriate.如果上下文需要区分,我们将酌情使用特定术语(静态字段、局部变量等)。You may also occasionally see the term "member" used as well.您偶尔也会看到使用“成员”一词。A type's fields, methods, and nested types are collectively called its members.类型的字段、方法和嵌套类型统称为其成员。
$
", or the underscore character "_
".$
”或下划线字符“_
”开头。$
" or "_
".$
”或“_
”。_
", this practice is discouraged._
”开头是合法的,但不鼓励这种做法。cadence
, speed
, and gear
, for example, are much more intuitive than abbreviated versions, such as s
, c
, and g
.cadence
、speed
和gear
的字段比缩写版本(如s
、c
和g
)直观得多。gearRatio
and currentGear
are prime examples of this convention.gearRatio
和currentGear
是此约定的主要示例。static final int NUM_GEARS = 6
, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character.static final int NUM_GEARS=6
,则约定会略有更改,将每个字母大写,并用下划线字符分隔后续单词。