📄 concepts.doc.html
字号:
<a name="26131"></a>A field, method, or constructor of a class may be declared using at most one of the <code>public</code>, <code>private</code>, or <code>protected</code> keywords. A <code>public</code> member may be accessed by any Java code. A <code>private</code> member may be accessed only from within the class that contains its declaration. A member that is not declared <code>public</code>, <code>protected</code>, or <code>private</code> is said to have <i>default access</i> and may be accessed from, and only from, anywhere in the package in which it is declared.<p><a name="26127"></a>A <code>protected</code> member of an object may be accessed only by code responsible for the implementation of that object. To be precise, a <code>protected</code> member may be accessed from anywhere in the package in which it is declared and, in addition, it may be accessed from within any declaration of a subclass of the class type that contains its declaration, provided that certain restrictions are obeyed. <p><a name="20207"></a><h3>2.7.9 Fully Qualified Names</h3><a name="26136"></a>Every package, class, interface, array type, and primitive type has a fully qualified name. It follows that every type except the null type has a fully qualified name.<p><ul><a name="26137"></a><li>The fully qualified name of a primitive type is the keyword for that primitive type, namely <code>boolean</code>, <code>char</code>, <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, or <code>double</code>.<a name="26138"></a><li>The fully qualified name of a named package that is not a subpackage of a named package is its simple name.<a name="26139"></a><li>The fully qualified name of a named package that is a subpackage of another named package consists of the fully qualified name of the containing package followed by "<code>.</code>" followed by the simple (member) name of the subpackage.<a name="26140"></a><li>The fully qualified name of a class or interface that is declared in an unnamed package is the simple name of the class or interface.<a name="26141"></a><li>The fully qualified name of a class or interface that is declared in a named package consists of the fully qualified name of the package followed by "<code>.</code>" followed by the simple name of the class or interface.<a name="26142"></a><li>The fully qualified name of an array type consists of the fully qualified name of the component type of the array type followed by "<code>[]</code>".</ul><a name="16198"></a><hr><h2>2.8 Classes</h2><a name="16202"></a>A <i>class declaration </i>specifies a new reference type and provides its implementation. Each class is implemented as an extension or subclass of a single existing class. A class may also implement one or more interfaces.<p><a name="18225"></a>The body of a class declares members (fields and methods), static initializers, and constructors.<p><a name="20389"></a><h3>2.8.1 Class Names</h3><a name="23195"></a>If a class is declared in a named package with the fully qualified name P, then the class has the fully qualified name P<code>.</code><i>Identifier</i>. If the class is in an unnamed package, then the class has the fully qualified name <i>Identifier</i>.<p><a name="23744"></a>Two classes are the <i>same class</i> (and therefore the <i>same type</i>) if they are loaded by the same class loader <a href="Concepts.doc.html#19175">(§2.16.2)</a> and they have the same fully qualified name <a href="Concepts.doc.html#20207">(§2.7.9)</a>.<p><a name="20340"></a><h3>2.8.2 Class Modifiers</h3><a name="20346"></a>A class declaration may include <i>class modifiers</i>. A class may be declared <code>public</code>, as discussed in <a href="Concepts.doc.html#18914">§2.7.8</a>.<p><a name="20341"></a>An <code>abstract</code> class is a class which is incomplete, or considered incomplete. Only <code>abstract</code> classes may have <code>abstract</code> methods <a href="Concepts.doc.html#16348">(§2.10.3)</a>, that is, methods which are declared but not yet implemented.<p><a name="20342"></a>A class can be declared <code>final</code> if its definition is complete and no subclasses are desired or required. Because a <code>final</code> class never has any subclasses, the methods of a <code>final</code> class cannot be overridden in a subclass. A class cannot be both <code>final</code> and <code>abstract</code>, because the implementation of such a class could never be completed.<p><a name="20343"></a>A class is declared <code>public</code> to make its type available to packages other than the one in which it is declared. A <code>public</code> class is accessible from other packages, using either its fully qualified name or a shorter name created by an <code>import</code> declaration <a href="Concepts.doc.html#20319">(§2.7.2)</a>, whenever the host permits access to its package. If a class lacks the <code>public</code> modifier, access to the class declaration is limited to the package in which it is declared.<p><a name="20358"></a><h3>2.8.3 Superclasses and Subclasses</h3><a name="20344"></a>The optional <code>extends</code> clause in a class declaration specifies the <i>direct superclass</i> of the current class, the class from whose implementation the implementation of the current class is derived. A class is said to be a <i>direct subclass</i> of the class it <code>extends</code>. Only the class <code>Object</code> <a href="Concepts.doc.html#27433">(§2.4.6)</a> has no direct superclass. If the <code>extends</code> clause is omitted from a class declaration, then the superclass of the new class is <code>Object</code>.<p><a name="20359"></a>The <i>subclass</i> relationship is the transitive closure of the direct subclass relationship. A class A is a subclass of a class C if A is a direct subclass of C, or if there is a direct subclass B of C and class A is a subclass of B. Class A is said to be a <i>superclass</i> of class C whenever C is a subclass of A.<p><a name="18846"></a><h3>2.8.4 The Class Members</h3><a name="18847"></a>The members of a class type include all of the following:<p><ul><a name="18848"></a><li>Members inherited from its direct superclass <a href="Concepts.doc.html#20358">(§2.8.3)</a>, except in class <code>Object</code>, which has no direct superclass.<a name="18849"></a><li>Members inherited from any direct superinterfaces <a href="Concepts.doc.html#20603">(§2.13.2)</a>.<a name="18850"></a><li>Members declared in the body of the class.</ul><a name="20425"></a>Members of a superclass that are declared <code>private</code> are not inherited by subclasses of that class. Members of a class that are not declared <code>private</code>, <code>protected</code>, or <code>public</code> are not inherited by subclasses declared in a package other than the one in which the class is declared. Constructors <a href="Concepts.doc.html#16411">(§2.12)</a> and static initializers <a href="Concepts.doc.html#16396">(§2.11)</a> are not members and therefore are not inherited.<p><a name="16338"></a><hr><h2>2.9 Fields</h2><a name="26290"></a>The variables of a class type are its <i>fields</i>. Class (<code>static</code>) variables exist once per class. Instance variables exist once per instance of the class. Fields may include initializers and may be modified using various modifier keywords. <p><a name="20543"></a>If the class declares a field with a certain name, then the declaration of that field is said to <i>hide</i> any and all accessible declarations of fields with the same name in the superclasses and superinterfaces of the class. A class inherits from its direct superclass and direct superinterfaces all the fields of the superclass and superinterfaces that are accessible to code in the class and are not hidden by a declaration in the class. A hidden field can be accessed by using a qualified name (if it is <code>static</code>) or by using a field access expression that contains a cast to a superclass type or the keyword <code>super</code>.<p><a name="16297"></a><h3>2.9.1 Field Modifiers</h3><a name="18302"></a>Fields may be declared <code>public</code>, <code>protected</code>, or <code>private</code>, as discussed in <a href="Concepts.doc.html#18914">§2.7.8</a>.<p><a name="26364"></a>If a field is declared <code>static</code>, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A <code>static</code> field, sometimes called a <i>class variable</i>, is incarnated when the class is initialized <a href="Concepts.doc.html#19075">(§2.16.4)</a>.<p><a name="26398"></a>A field that is not declared <code>static</code> is called an <i>instance variable</i>. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or in any of its superclasses. <p><a name="26395"></a>A field can be declared <code>final</code>, in which case its declarator must include a variable initializer <a href="Concepts.doc.html#16320">(§2.9.2)</a>. Both class and instance variables (<code>static</code> and non-<code>static</code> fields) may be declared <code>final</code>. Once a <code>final</code> field has been initialized, it always contains the same value. If a <code>final</code> field holds a reference to an object, then the state of the object may be changed by operations on the object, but the field will always refer to the same object.<p><a name="18858"></a>Variables may be marked <code>transient</code> to indicate that they are not part of the persistent state of an object. The <code>transient</code> attribute can be used by a Java implementation to support special system services. <i>The Java Language Specification</i> does not yet specify details of such services.<p><a name="18279"></a>The Java language allows threads that access shared variables to keep private working copies of the variables; this allows a more efficient implementation of multiple threads <a href="Concepts.doc.html#24465">(§2.17)</a>. These working copies need be reconciled with the master copies in the shared main memory only at prescribed synchronization points, namely when objects are locked or unlocked <a href="Concepts.doc.html#24465">(§2.17)</a>. As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive access to such variables by obtaining a lock that conventionally enforces mutual exclusion for those shared variables.<p><a name="26427"></a>Java provides a second mechanism that is more convenient for some purposes: a field may be declared <code>volatile</code>, in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested. A <code>final</code> field cannot also be declared <code>volatile</code>.<p><a name="16320"></a><h3>2.9.2 Initialization of Fields</h3><a name="16324"></a>If a field declaration contains a variable initializer, then it has the semantics of an assignment to the declared variable, and:<p><ul><a name="16328"></a><li>If the declaration is for a class variable (that is, a <code>static</code> field), then the variable initializer is evaluated and the assignment performed exactly once, when the class is initialized <a href="Concepts.doc.html#19075">(§2.16.4)</a>.<a name="16332"></a><li>If the declaration is for an instance variable (that is, a field that is not <code>static</code>), then the variable initializer is evaluated and the assignment performed each time an instance of the class is created.</ul><a name="16317"></a><hr><h2>2.10 Methods</h2><a name="17564"></a>A <i>method </i>declares executable code that can be invoked, passing a fixed number of values as arguments. Every method declaration belongs to some class. A class inherits from its direct superclass <a href="Concepts.doc.html#20358">(§2.8.3)</a> and any direct superinterfaces <a href="Concepts.doc.html#20603">(§2.13.2)</a> all the accessible methods of the superclass and superinterfaces, with one exception: if a name is declared as a method in the new class, then no method with the same signature <a href="Concepts.doc.html#18290">(§2.10.2)</a> is inherited. Instead, the newly declared method is said to <i>override</i> any such method declaration. An overriding method must not conflict with the definition that it overrides, for instance, by having a different return type. Overridden methods of the superclass can be accessed using a method invocation expression involving the <code>super</code> keyword.<p><a name="26454"></a><h3>2.10.1 Formal Parameters</h3><a name="26455"></a>The <i>formal parameters</i> of a method, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type and an identifier that specifies the name of the parameter. When the method is invoked, the values of the actual argument expressions initialize newly created parameter variables <a href="Concepts.doc.html#17203">(§2.5)</a>, each of the declared type<i>,</i> before execution of the body of the method.<p><a name="18290"></a><h3>2.10.2 Signature</h3><a name="18291"></a>The <i>signature</i> of a method consists of the name of the method and the number and type of formal parameters <a href="Concepts.doc.html#26454">(§2.10.1)</a> to the method. A class may not declare two methods with the same signature. <p><a name="16348"></a><h3>2.10.3 Method Modifiers<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -