📄 8.doc.html
字号:
<html>
<head>
<title>The Java Language Specification Classes</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="7.doc.html">Prev</a> | <a href="9.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="3857"></a>
<p><strong>
CHAPTER 8 </strong></p>
<a name="44365"></a>
<h1>Classes</h1>
<hr><p>
<a name="228205"></a>
Class declarations define new reference types and describe how they are
implemented <a href="8.doc.html#15372">(§8.1)</a>.
<p><a name="228209"></a>
The name of a class has as its scope all type declarations in the package in which the class is declared <a href="8.doc.html#39196">(§8.1.1)</a>. A class may be declared <code>abstract</code> <a href="8.doc.html#34944">(§8.1.2.1)</a> and must be declared <code>abstract</code> if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses. A class may be declared <code>final</code> <a href="8.doc.html#54727">(§8.1.2.2)</a>, in which case it cannot have subclasses. If a class is declared <code>public</code>, then it can be referred to from other packages.<p>
<a name="35674"></a>
Each class except <code>Object</code> is an extension of (that is, a subclass of) a single existing class <a href="8.doc.html#21723">(§8.1.3)</a> and may implement interfaces <a href="8.doc.html#34031">(§8.1.4)</a>.<p>
<a name="15689"></a>
The body of a class declares members (fields and methods), static initializers, and constructors <a href="8.doc.html#18988">(§8.1.5)</a>. The scope of the name of a member is the entire declaration of the class to which the member belongs. Field, method, and constructor declarations may include the access modifiers <a href="6.doc.html#33916">(§6.6)</a> <code>public</code>, <code>protected</code>, or <code>private</code>. The members of a class include both declared and inherited members <a href="8.doc.html#21831">(§8.2)</a>. Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface.<p>
<a name="15807"></a>
Field declarations <a href="8.doc.html#40898">(§8.3)</a> describe class variables, which are incarnated once, and instance variables, which are freshly incarnated for each instance of the class. A field may be declared <code>final</code> <a href="8.doc.html#35962">(§8.3.1.2)</a>, in which case it cannot be assigned to except as part of its declaration. Any field declaration may include an initializer; the declaration of a <code>final</code> field must include an initializer.<p>
<a name="35821"></a>
Method declarations <a href="8.doc.html#40420">(§8.4)</a> describe code that may be invoked by method invocation expressions <a href="15.doc.html#20448">(§15.11)</a>. A class method is invoked relative to the class type; an instance method is invoked with respect to some particular object that is an instance of the class type. A method whose declaration does not indicate how it is implemented must be declared <code>abstract</code>. A method may be declared <code>final</code> <a href="8.doc.html#11246">(§8.4.3.3)</a>, in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent <code>native</code> code <a href="8.doc.html#31125">(§8.4.3.4)</a>. A <code>synchronized</code> method <a href="8.doc.html#55408">(§8.4.3.5)</a> automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a <code>synchronized</code> statement <a href="14.doc.html#79287">(§14.17)</a>, thus allowing its activities to be synchronized with those of other threads <a href="17.doc.html#26250">(§17)</a>.<p>
<a name="227762"></a>
Method names may be overloaded <a href="8.doc.html#227768">(§8.4.7)</a>.<p>
<a name="35837"></a>
Static initializers <a href="8.doc.html#39245">(§8.5)</a> are blocks of executable code that may be used to help initialize a class when it is first loaded <a href="12.doc.html#44557">(§12.4)</a>.<p>
<a name="35735"></a>
Constructors <a href="8.doc.html#41652">(§8.6)</a> are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances. Like methods, they may be overloaded <a href="8.doc.html#229266">(§8.6.6)</a>.<p>
<a name="15372"></a>
<h2>8.1 Class Declaration</h2>
<a name="23643"></a>
A <i>class declaration</i> specifies a new reference type:
<p><ul><pre>
<i>ClassDeclaration:<br>
</i> <i>ClassModifiers</i><sub><i>opt</i></sub><code> class </code><i>Identifier</i><code> </code><i>Super</i><sub><i>opt</i></sub><code> </code><i>Interfaces</i><sub><i>opt</i></sub><code> </code><i>ClassBody
</i></pre></ul><a name="29516"></a>
If a class is declared in a named package <a href="7.doc.html#26621">(§7.4.1)</a> with fully qualified name <i>P</i>
<a href="6.doc.html#25430">(§6.7)</a>, then the class has the fully qualified name <i>P</i><code>.</code><i>Identifier</i>. If the class is in an
unnamed package <a href="7.doc.html#26639">(§7.4.2)</a>, then the class has the fully qualified name <i>Identifier</i>.
In the example:
<p><pre><a name="29524"></a>class Point { int x, y; }
</pre><a name="29525"></a>
the class <code>Point</code> is declared in a compilation unit with no <code>package</code> statement, and
thus <code>Point</code> is its fully qualified name, whereas in the example:
<p><pre><a name="29526"></a>
package vista;
<a name="29527"></a>class Point { int x, y; }
</pre><a name="29528"></a>
the fully qualified name of the class <code>Point</code> is <code>vista.Point</code>. (The package name
<code>vista</code> is suitable for local or personal use; if the package were intended to be
widely distributed, it would be better to give it a unique package name <a href="7.doc.html#40169">(§7.7)</a>.)
<p><a name="77906"></a>
A compile-time error occurs if the <em>Identifier </em>naming a class appears as the name of any other class type or interface type declared in the same package <a href="7.doc.html#26783">(§7.6)</a>.<p>
<a name="28418"></a>
A compile-time error occurs if the <i>Identifier</i> naming a class is also declared as a type by a single-type-import declaration <a href="7.doc.html#26699">(§7.5.1)</a> in the compilation unit <a href="7.doc.html#40031">(§7.3)</a> containing the class declaration.<p>
<a name="227770"></a>
In the example:<p>
<pre><a name="18901"></a>
package test;
<br><a name="33758"></a>import java.util.Vector;
<br><a name="25534"></a>
class Point {
<a name="25538"></a> int x, y;
<a name="244854"></a>}
<br><a name="244855"></a>
interface Point { // compile-time error #1
<a name="244856"></a> int getR();
<a name="244821"></a> int getTheta();
<a name="25541"></a>}
<br><a name="18906"></a>class Vector { Point[] pts; } // compile-time error #2
</pre><a name="22257"></a>
the first compile-time error is caused by the duplicate declaration of the name
<code>Point</code> as both a <code>class</code> and an <code>interface</code> in the same package. A second error
detected at compile time is the attempt to declare the name <code>Vector</code> both by a class
type declaration and by a single-type-import declaration.
<p><a name="35887"></a>
Note, however, that it is not an error for the <i>Identifier</i> that names a class also to name a type that otherwise might be imported by a type-import-on-demand declaration <a href="7.doc.html#26725">(§7.5.2)</a> in the compilation unit <a href="7.doc.html#40031">(§7.3)</a> containing the class declaration. In the example:<p>
<pre><a name="35908"></a>
package test;
<br><a name="35909"></a>import java.util.*;
<br><a name="35917"></a>class Vector { Point[] pts; } // not a compile-time error
</pre><a name="35926"></a>
the declaration of the class <code>Vector</code> is permitted even though there is also a class
<code>java.util.Vector</code>. Within this compilation unit, the simple name <code>Vector</code> refers
to the class <code>test.Vector</code>, not to <code>java.util.Vector</code> (which can still be referred
to by code within the compilation unit, but only by its fully qualified name).
<p><a name="39196"></a>
<h3>8.1.1 Scope of a Class Type Name</h3>
<a name="39197"></a>
The <i>Identifier</i> in a class declaration specifies the name of the class. This class
name has as its scope <a href="6.doc.html#33623">(§6.3)</a> the entire package in which the class is declared. As
an example, the compilation unit:
<p><pre><br><a name="16470"></a>package points;
<br></pre><pre><a name="16471"></a>
class Point {
<a name="16472"></a> int x, y; // coordinates
<a name="16473"></a> PointColor color; // color of this point
<a name="16474"></a> Point next; // next point with this color<br>
static int nPoints;
<a name="16475"></a>}
<br><a name="16476"></a>
class PointColor {
<a name="16477"></a> Point first; // first point with this color
<a name="16478"></a> PointColor(int color) {
<a name="228374"></a> this.color = color;
<a name="228375"></a> }
<a name="16479"></a> private int color; // color components
<a name="16480"></a>}
</pre><a name="16486"></a>
defines two classes that use each other in the declarations of their class members.
Because the class type names <code>Point</code> and <code>PointColor</code> have the entire package
<code>points</code>, including the entire current compilation unit, as their scope, this example
compiles correctly-that is, forward reference is not a problem.
<p><a name="21613"></a>
<h3>8.1.2 Class Modifiers</h3>
<a name="54718"></a>
A class declaration may include <i>class modifiers</i>.
<p><ul><pre>
<i>ClassModifiers:<br>
ClassModifier<br>
ClassModifiers</i><code> </code><i>ClassModifier
</i>
<i>ClassModifier: one of<br>
</i><code>public</code><i> </i><code>abstract final
</code></pre></ul><a name="14169"></a>
The access modifier <code>public</code> is discussed in <a href="6.doc.html#33916">§6.6</a>. A compile-time error occurs if
the same modifier appears more than once in a class declaration. If two or more
class modifiers appear in a class declaration, then it is customary, though not
required, that they appear in the order consistent with that shown above in the production
for <i>ClassModifier</i>.
<p><a name="34944"></a>
<h4>8.1.2.1 abstract Classes</h4>
<a name="20269"></a>
An <code>abstract</code> class is a class that is incomplete, or to be considered incomplete.
Only <code>abstract</code> classes may have <code>abstract</code> methods (<a href="8.doc.html#34484">§8.4.3.1</a>, <a href="9.doc.html#78651">§9.4</a>), that is,
methods that are declared but not yet implemented. If a class that is not <code>abstract</code>
contains an <code>abstract</code> method, then a compile-time error occurs. A class has
<code>abstract</code> methods if any of the following is true:
<p><ul><a name="36417"></a>
<li>It explicitly contains a declaration of an <code>abstract</code> method <a href="8.doc.html#78188">(§8.4.3)</a>.
<a name="14240"></a>
<li>It inherits an <code>abstract</code> method from its direct superclass <a href="8.doc.html#21723">(§8.1.3)</a>.
<a name="14247"></a>
<li>A direct superinterface (<a href="8.doc.html#34031">§8.1.4</a>) of the class declares or inherits a method (which is therefore necessarily <code>abstract</code>) and the class neither declares nor inherits a method that implements it.
</ul><a name="54865"></a>
In the example:
<p><pre><a name="54866"></a>
abstract class Point {
<a name="54867"></a> int x = 1, y = 1;
<a name="54868"></a> void move(int dx, int dy) {
<a name="54869"></a> x += dx;
<a name="54870"></a> y += dy;
<a name="54871"></a> alert();
<a name="54872"></a> }
<a name="54873"></a> abstract void alert();
<a name="54874"></a>}
<br><a name="36437"></a>
abstract class ColoredPoint extends Point {
<a name="36438"></a> int color;
<a name="36439"></a>}
<br><a name="54875"></a>
class SimplePoint extends Point {
<a name="54876"></a> void alert() { }
<a name="54877"></a>}
</pre><a name="54878"></a>
a class <code>Point</code> is declared that must be declared <code>abstract</code>, because it contains a
declaration of an <code>abstract</code> method named <code>alert</code>. The subclass of <code>Point</code> named<code>
ColoredPoint</code> inherits the <code>abstract</code> method <code>alert</code>, so it must also be declared
<code>abstract</code>. On the other hand, the subclass of <code>Point</code> named <code>SimplePoint</code> provides
an implementation of <code>alert</code>, so it need not be <code>abstract</code>.
<p><a name="14273"></a>
A compile-time error occurs if an attempt is made to create an instance of an <code>abstract</code> class using a class instance creation expression <a href="15.doc.html#41147">(§15.8)</a>. An attempt to instantiate an <code>abstract</code> class using the <code>newInstance</code> method of class <code>Class</code> <a href="javalang.doc2.html#15088">(§20.3.6)</a> will cause an <code>InstantiationException</code> <a href="11.doc.html#44285">(§11.5.1)</a> to be thrown. Thus, continuing the example just shown, the statement:<p>
<pre><a name="54881"></a> Point p = new Point();
</pre><a name="54883"></a>
would result in a compile-time error; the class <code>Point</code> cannot be instantiated
because it is <code>abstract</code>. However, a <code>Point</code> variable could correctly be initialized
with a reference to any subclass of <code>Point</code>, and the class <code>SimplePoint</code> is not
<code>abstract</code>, so the statement:
<p><pre><a name="227781"></a> Point p = new SimplePoint();
</pre><a name="227782"></a>
would be correct.
<p><a name="227783"></a>
A subclass of an <code>abstract</code> class that is not itself <code>abstract</code> may be instantiated, resulting in the execution of a constructor for the <code>abstract</code> class and, therefore, the execution of the field initializers for instance variables of that class. Thus, in the example just given, instantiation of a <code>SimplePoint</code> causes the default constructor and field initializers for <code>x</code> and <code>y</code> of <code>Point</code> to be executed.<p>
<a name="54900"></a>
It is a compile-time error to declare an <code>abstract</code> class type such that it is not possible to create a subclass that implements all of its <code>abstract</code> methods. This situation can occur if the class would have as members two <code>abstract</code> methods that have the same method signature <a href="8.doc.html#38649">(§8.4.2)</a> but different return types. As an example, the declarations:<p>
<pre><a name="54908"></a>
interface Colorable { void setColor(int color); }
<a name="54909"></a>
abstract class Colored implements Colorable {
<a name="54910"></a> abstract int setColor(int color);
<a name="54931"></a>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -