⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="32233"></a>
<li>As the class type of an instance that is to be created in a class instance creation expression <a href="15.doc.html#41147">(&#167;15.8)</a>
<a name="32240"></a>
<li>As the element type of an array to be created in an array creation expression <a href="15.doc.html#46168">(&#167;15.9)</a>
<a name="32244"></a>
<li>As the type mentioned in the cast operator of a cast expression <a href="15.doc.html#238146">(&#167;15.15)</a>
<a name="32248"></a>
<li>As the type that follows the <code>instanceof</code> relational operator <a href="15.doc.html#80289">(&#167;15.19.2)</a>
</ul>
</ul><a name="32435"></a>
A name is syntactically classified as an <i>ExpressionName</i> in these contexts:
<p><ul><a name="32464"></a>
<li>As the array reference expression in an array access expression <a href="15.doc.html#239587">(&#167;15.12)</a>
<a name="32459"></a>
<li>As a <i>PostfixExpression</i> <a href="15.doc.html#36254">(&#167;15.13)</a>
<a name="32477"></a>
<li>As the left-hand operand of an assignment operator <a href="15.doc.html#5281">(&#167;15.25)</a>
</ul><a name="32437"></a>
A name is syntactically classified as a <i>MethodName</i> in this context:
<p><ul><a name="32480"></a>
<li>Before the "<code>(</code>" in a method invocation expression <a href="15.doc.html#20448">(&#167;15.11)</a>
</ul><a name="32439"></a>
A name is syntactically classified as an <i>AmbiguousName</i> in these contexts:
<p><ul><a name="32496"></a>
<li>To the left of the "<code>.</code>" in a qualified <i>ExpressionName</i>
<a name="32499"></a>
<li>To the left of the "<code>.</code>" in a qualified <i>MethodName</i>
<a name="32501"></a>
<li>To the left of the "<code>.</code>" in a qualified <i>AmbiguousName</i>
</ul><a name="32725"></a>
<h3>6.5.2    Reclassification of Contextually Ambiguous Names</h3>
<a name="32133"></a>
An <i>AmbiguousName</i> is then reclassified as follows:
<p><ul><a name="32505"></a>
<li>If the <i>AmbiguousName</i> is a simple name, consisting of a single <i>Identifier</i>:
<ul>
<a name="32544"></a>
<li>If the <i>Identifier</i> appears within the scope <a href="6.doc.html#33623">(&#167;6.3)</a> of a local variable declaration <a href="14.doc.html#5920">(&#167;14.3)</a> or parameter declaration (<a href="8.doc.html#38698">&#167;8.4.1</a>, <a href="8.doc.html#29488">&#167;8.6.1</a>, <a href="14.doc.html#79311">&#167;14.18</a>) with that name, then the <i>AmbiguousName</i> is reclassified as an <i>ExpressionName</i>.
<a name="32569"></a>
<li>Otherwise, consider the class or interface <i>C</i> within whose declaration the <i>Identifier</i> occurs. If <i>C</i> has one or more fields with that name, which may be either declared within it or inherited, then the <i>AmbiguousName</i> is reclassified as an <i>ExpressionName</i>.
<a name="32843"></a>
<li>Otherwise, if a type of that name is declared in the compilation unit <a href="7.doc.html#40031">(&#167;7.3)</a> containing the <i>Identifier</i>, either by a single-type-import declaration <a href="7.doc.html#26699">(&#167;7.5.1)</a> or by a class or interface type declaration <a href="7.doc.html#26783">(&#167;7.6)</a>, then the <i>AmbiguousName</i> is reclassified as a <i>TypeName</i>.
<a name="32853"></a>
<li>Otherwise, if a type of that name is declared in another compilation unit <a href="7.doc.html#40031">(&#167;7.3)</a> of the package <a href="7.doc.html#26535">(&#167;7.1)</a> of the compilation unit containing the <i>Identifier</i>, &#32;then the <i>AmbiguousName</i> is reclassified as a <i>TypeName</i>.
<a name="32860"></a>
<li>Otherwise, if a type of that name is declared by exactly one type-import-on-demand declaration <a href="7.doc.html#26725">(&#167;7.5.2)</a> of the compilation unit containing the <i>Identifier</i>, &#32;then the <i>AmbiguousName</i> is reclassified as a <i>TypeName</i>.
<a name="32864"></a>
<li>Otherwise, if a type of that name is declared by more than one type-import-on-demand declaration of the compilation unit containing the <i>Identifier</i>, then a compile-time error results.
<a name="32535"></a>
<li>Otherwise, the <i>AmbiguousName</i> is reclassified as a <i>PackageName</i>. A later step determines whether or not a package of that name actually exists.
</ul>
<a name="32506"></a>
<li>If the <i>AmbiguousName</i> is a qualified name, consisting of a name, a "<code>.</code>", and an <i>Identifier</i>, then the name to the left of the "<code>.</code>" is first reclassified, for it is itself an <i>AmbiguousName</i>. There is then a choice:
<ul>
<a name="32509"></a>
<li>If the name to the left of the "<code>.</code>" is reclassified as a <i>PackageName</i>, then there is a further choice:
<ul>
<a name="32700"></a>
<li>If there is a package whose name is the name to the left of the "<code>.</code>" and that package contains a declaration of a type whose name is the same as the <i>Identifier</i>, then this <i>AmbiguousName</i> is reclassified as a <i>TypeName</i>.
<a name="32713"></a>
<li>Otherwise, this <i>AmbiguousName</i> is reclassified as a <i>PackageName</i>. A later step determines whether or not a package of that name actually exists.
</ul>
<a name="32511"></a>
<li>If the name to the left of the "<code>.</code>" is reclassified as a <i>TypeName</i>, then this <i>AmbiguousName</i> is reclassified as an <i>ExpressionName</i>.
<a name="32513"></a>
<li>If the name to the left of the "<code>.</code>" is reclassified as an <i>ExpressionName</i>, then this <i>AmbiguousName</i> is reclassified as an <i>ExpressionName</i>.
</ul>
</ul><a name="60456"></a>
As an example, consider the following contrived "library code":<p>
<pre><br><a name="60457"></a>package ORG.rpgpoet;
<br><br><a name="60459"></a>import java.util.Random;
<br><a name="60458"></a>interface Music { Random[] wizards = new Random[4]; }
</pre><a name="60464"></a>
and then consider this example code in another package:
<p><pre><br><a name="60521"></a>package bazola;
<br></pre><pre><a name="60467"></a>
class Gabriel {
<a name="60466"></a>	static int n = ORG.rpgpoet.Music.wizards.length;
<a name="60465"></a>}
</pre><a name="60483"></a>
First of all, the name <code>ORG.rpgpoet.Music.wizards.length</code> is classified as an 
<i>ExpressionName</i> because it functions as a <i>PostfixExpression</i>. Therefore, each of 
the names:
<p><pre><a name="60488"></a>
ORG.rpgpoet.Music.wizards
<a name="60490"></a>ORG.rpgpoet.Music
<a name="60492"></a>ORG.rpgpoet
<a name="60494"></a>ORG
</pre><a name="60515"></a>
is initially classified as an <i>AmbiguousName</i>. These are then reclassified:
<p><ul><a name="60526"></a>
<li>Assuming that there is no class or interface named <code>ORG</code> in any other compilation unit of package <code>bazola</code>, then the simple name <code>ORG</code> is reclassified as a <i>PackageName</i>.
<a name="60530"></a>
<li>Next, assuming that there is no class or interface named <code>rpgpoet</code> in any compilation unit of package <code>ORG</code> (and we know that there is no such class or interface because package <code>ORG</code> has a subpackage named <code>rpgpoet</code>), the qualified name <code>ORG.rpgpoet</code> is reclassified as a <i>PackageName</i>.
<a name="60542"></a>
<li>Next, because package <code>ORG.rpgpoet</code> has an interface type named <code>Music</code>, the qualified name <code>ORG.rpgpoet.Music</code> is reclassified as a <i>TypeName</i>.
<a name="60540"></a>
<li>Finally, because the name <code>ORG.rpgpoet.Music</code> is a <i>TypeName</i>, the qualified name <code>ORG.rpgpoet.Music.wizards</code> is reclassified as an <i>ExpressionName</i>.
</ul><a name="22349"></a>
<h3>6.5.3    Meaning of Package Names</h3>
<a name="32757"></a>
The meaning of a name classified as a <i>PackageName</i> is determined as follows.
<p><a name="22481"></a>
<h4>6.5.3.1    Simple Package Names</h4>
<a name="21719"></a>
If a package name consists of a single <i>Identifier</i>, then this identifier denotes a top-
level package named by that identifier. If no package of that name is accessible, as 
determined by the host system <a href="7.doc.html#13180">(&#167;7.4.3)</a>, then a compile-time error occurs.
<p><a name="22482"></a>
<h4>6.5.3.2    Qualified Package Names</h4>
<a name="21727"></a>
If a package name is of the form <i>Q</i><code>.</code><i>Id</i>, then <i>Q</i> must also be a package name. The 
package name <i>Q</i><code>.</code><i>Id</i> names a package that is the member named <i>Id</i> within the 
package named by <i>Q</i>. If <i>Q</i> does not name an accessible package or <i>Id</i> does not 
name an accessible subpackage of that package, then a compile-time error occurs.
<p><a name="21721"></a>
<h3>6.5.4    Meaning of Type Names</h3>
<a name="32763"></a>
The meaning of a name classified as a <i>TypeName</i> is determined as follows.
<p><a name="21810"></a>
<h4>6.5.4.1    Simple Type Names</h4>
<a name="32799"></a>
If a type name consists of a single <i>Identifier</i>, then the identifier must occur in the 
scope of a declaration of a type with this name, or a compile-time error occurs. It 
is possible that the identifier occurs within the scope of more than one type with 
that name, in which case the type denoted by the name is determined as follows:
<p><ul><a name="21741"></a>
<li>If a type with that name is declared in the current compilation unit <a href="7.doc.html#40031">(&#167;7.3)</a>, either by a single-type-import declaration <a href="7.doc.html#26699">(&#167;7.5.1)</a> or by a declaration of a class or interface &#32;type <a href="7.doc.html#26783">(&#167;7.6)</a>, then the simple type name denotes that type.
<a name="21751"></a>
<li>Otherwise, if a type with that name is declared in another compilation unit <a href="7.doc.html#40031">(&#167;7.3)</a> of the package <a href="7.doc.html#26535">(&#167;7.1)</a> containing the identifier, then the identifier denotes that type. Note that, in systems that store compilation units in a file system, such a compilation unit must have a file name that is the name of the type <a href="7.doc.html#26783">(&#167;7.6)</a>.
<a name="21758"></a>
<li>Otherwise, if a type of that name is declared by exactly one type-import-on-demand declaration <a href="7.doc.html#26725">(&#167;7.5.2)</a> of the compilation unit containing the identifier, then the simple type name denotes that type.
<a name="32932"></a>
<li>Otherwise, if a type of that name is declared by more than one type-import-on-demand declaration of the compilation unit, then the name is ambiguous as a type name; a compile-time error occurs.
<a name="32933"></a>
<li>Otherwise, the name is undefined as a type name; a compile-time error occurs.
</ul><a name="32934"></a>
This order for considering type declarations is designed to choose the most 
explicit of two or more applicable type declarations.
<p><a name="21811"></a>
<h4>6.5.4.2    Qualified Type Names</h4>
<a name="32818"></a>
If a type name is of the form <i>Q</i><code>.</code><i>Id</i>, then <i>Q</i> must be a package name. The type 
name <i>Q</i><code>.</code><i>Id</i> names a type that is the member named <i>Id</i> within the package named 
by <i>Q</i>. If <i>Q</i> does not name an accessible package, or <i>Id</i> does not name a type within 
that package, or the type named <i>Id</i> within that package is not accessible <a href="6.doc.html#33916">(&#167;6.6)</a>, 
then a compile-time error occurs.
<p><a name="32816"></a>
The example:<p>
<pre><a name="21789"></a>package wnj.test;
</pre><pre><a name="21790"></a>
class Test {
<a name="21791"></a>	public static void main(String[] args) {
<a name="21792"></a>		java.util.Date date =
<a name="21793"></a>			new java.util.Date(System.currentTimeMillis());
<a name="21794"></a>		System.out.println(date.toLocaleString());
<a name="21795"></a>	}
<a name="21796"></a>}
</pre><a name="21797"></a>
produced the following output the first time it was run:
<p><pre><a name="21798"></a>Sun Jan 21 22:56:29 1996
</pre><a name="21799"></a>
In this example:
<p><ul><a name="21800"></a>
<li>The name <code>wnj.test</code> must name a package on the host system. It is resolved by first looking for the package <code>wnj</code>, using the procedure described in <a href="6.doc.html#22481">&#167;6.5.3.1</a>, and then making sure that the subpackage <code>test</code> of this package is accessible.
<a name="21807"></a>
<li>The name <code>java.util.Date</code> <a href="javautil.doc2.html#7433">(&#167;21.3)</a> must denote a type, so we first use the procedure recursively to determine if <code>java.util</code> is an accessible package, which it is, and then look to see if the type <code>Date</code> is accessible in this package.
</ul><a name="21650"></a>
<h3>6.5.5    Meaning of Expression Names</h3>
<a name="32767"></a>
The meaning of a name classified as an <i>ExpressionName</i> is determined as follows.
<p><a name="22022"></a>
<h4>6.5.5.1    Simple Expression Names</h4>
<a name="22023"></a>
If an expression name consists of a single <i>Identifier</i>, then:
<p><ul><a name="22030"></a>
<li>If the <i>Identifier</i> appears within the scope <a href="6.doc.html#33623">(&#167;6.3)</a> of a local variable declaration <a href="14.doc.html#5920">(&#167;14.3)</a> or parameter declaration (<a href="8.doc.html#38698">&#167;8.4.1</a>, <a href="8.doc.html#29488">&#167;8.6.1</a>, <a href="14.doc.html#79311">&#167;14.18</a>) with that name, then the expression name denotes a variable, that is, that local variable or parameter. Local variables and parameters are never hidden (<a href="6.doc.html#33623">&#167;6.3</a>, <a href="6.doc.html#34133">&#167;6.3.1</a>, <a href="14.doc.html#5920">&#167;14.3</a>), so there is necessarily at most one such local variable or parameter. The type of the expression name is the declared type of the local variable or parameter.
<a name="22052"></a>
<li>Otherwise, if the <i>Identifier</i> appears within a class declaration <a href="8.doc.html#3857">(&#167;8)</a>:
<ul>
<a name="22062"></a>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -