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发行说明。
A class declaration names the class and encloses the class body between braces.类声明命名类并将类主体括在大括号之间。The class name can be preceded by modifiers.类名前面可以有修饰符。The class body contains fields, methods, and constructors for the class.类主体包含类的字段、方法和构造函数。A class uses fields to contain state information and uses methods to implement behavior.类使用字段来包含状态信息,并使用方法来实现行为。Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.初始化类的新实例的构造函数使用类的名称,看起来像没有返回类型的方法。
You control access to classes and members in the same way: by using an access modifier such as 通过在类和成员的声明中使用访问修饰符(如public
in their declaration.public
)来控制对类和成员的访问。
You specify a class variable or a class method by using the 通过在成员声明中使用static
keyword in the member's declaration.static
关键字来指定类变量或类方法。A member that is not declared as 未声明为static
is implicitly an instance member.static
的成员隐式地是实例成员。Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference.类变量由类的所有实例共享,可以通过类名和实例引用进行访问。Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.类的实例获取每个实例变量各自的副本,必须通过实例引用访问这些副本。
You create an object from a class by using the 通过使用new
operator and a constructor.new
运算符和构造函数从类创建对象。The new operator returns a reference to the object that was created.新操作符返回对已创建对象的引用。You can assign the reference to a variable or use it directly.可以将引用指定给变量,也可以直接使用它。
Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name.可以使用限定名引用在声明它们的类之外的代码可以访问的实例变量和方法。The qualified name of an instance variable looks like this:实例变量的限定名称如下所示:
objectReference.variableName
The qualified name of a method looks like this:方法的限定名称如下所示:
objectReference.methodName(argumentList)
or:或:
objectReference.methodName()
The garbage collector automatically cleans up unused objects.垃圾收集器会自动清理未使用的对象。An object is unused if the program holds no more references to it.如果程序不再持有对某个对象的引用,则该对象未被使用。You can explicitly drop a reference by setting the variable holding the reference to 通过将保存引用的变量设置为null
.null
,可以显式删除引用。