📄 6.doc.html
字号:
<html>
<head>
<title>The Java Language Specification Names</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="5.doc.html">Prev</a> | <a href="7.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="48086"></a>
<p><strong>
CHAPTER 6 </strong></p>
<a name="44352"></a>
<h1>Names</h1>
<hr><p>
<a name="10551"></a>
Names are used to refer to entities declared in a Java program. A declared
entity <a href="6.doc.html#33757">(§6.1)</a> is a package, class type, interface type, member (field or method) of a
reference type, parameter (to a method, constructor, or exception handler), or
local variable.
<p><a name="56343"></a>
Names in Java programs are either simple, consisting of a single identifier, or qualified,  consisting of a sequence of identifiers separated by "<code>.</code>" tokens <a href="6.doc.html#31692">(§6.2)</a>.<p>
<a name="56234"></a>
Every name introduced by a declaration has a <i>scope</i> <a href="6.doc.html#33623">(§6.3)</a>, which is the part of the Java program text within which the declared entity can be referred to by a simple name.<p>
<a name="56241"></a>
Packages and reference types (that is, class types, interface types, and array types) have members <a href="6.doc.html#31814">(§6.4)</a>. A member can be referred to using a qualified name <i>N</i><code>.</code><i>x</i>, where <i>N</i> is a simple or qualified name and <i>x</i> is an identifier. If <i>N</i> names a package, then <i>x</i> is a member of that package, which is either a class or interface type or a subpackage. If <i>N</i> names a reference type or a variable of a reference type, then <i>x</i> names a member of that type, which is either a field or a method.<p>
<a name="34249"></a>
In determining the meaning of a name <a href="6.doc.html#20569">(§6.5)</a>, Java uses the context of the occurrence to disambiguate among packages, types, variables, and methods with the same name.<p>
<a name="56266"></a>
Access control <a href="6.doc.html#33916">(§6.6)</a> can be specified in a class, interface, method, or field declaration to control when <i>access</i> to a member is allowed. Access is a different concept from scope; access specifies the part of the Java program text within which the declared entity can be referred to by a qualified name, a field access expression <a href="15.doc.html#41267">(§15.10)</a>, or a method invocation expression <a href="15.doc.html#20448">(§15.11)</a> in which the method is not specified by a simple name. The default access is that a member can be accessed anywhere within the package that contains its declaration; other possibilities are <code>public</code>, <code>protected</code>, and <code>private</code>.<p>
<a name="27329"></a>
Fully qualified names <a href="6.doc.html#25430">(§6.7)</a> and naming conventions <a href="6.doc.html#11186">(§6.8)</a> are also discussed in this chapter.<p>
<a name="31249"></a>
The name of a field, parameter, or local variable may be used as an expression <a href="15.doc.html#4984">(§15.13.1)</a>. The name of a method may appear in an expression only as part of a method invocation expression <a href="15.doc.html#20448">(§15.11)</a>. The name of a class or interface type may appear in an expression only as part of a class instance creation expression <a href="15.doc.html#41147">(§15.8)</a>, an array creation expression <a href="15.doc.html#46168">(§15.9)</a>, a cast expression <a href="15.doc.html#238146">(§15.15)</a>, or an <code>instanceof</code> expression <a href="15.doc.html#80289">(§15.19.2)</a>, or as part of a qualified name for a field or method. The name of a package may appear in an expression only as part of a qualified name for a class or interface type.<p>
<a name="33757"></a>
<h2>6.1 Declarations</h2>
<a name="33759"></a>
A <i>declaration</i> introduces an entity into a Java program and includes an identifier
<a href="3.doc.html#40625">(§3.8)</a> that can be used in a name to refer to this entity. A declared entity is one of
the following:
<p><ul><a name="60314"></a>
<li>A package, declared in a <code>package</code> declaration <a href="7.doc.html#26619">(§7.4)</a>
<a name="60319"></a>
<li>An imported type, declared in a single-type-import declaration <a href="7.doc.html#26699">(§7.5.1)</a> or a type-import-on-demand declaration <a href="7.doc.html#26725">(§7.5.2)</a>
<a name="27103"></a>
<li>A class, declared in a class type declaration <a href="8.doc.html#15372">(§8.1)</a>
<a name="27107"></a>
<li>An interface, declared in an interface type declaration <a href="9.doc.html#35470">(§9.1)</a>
<a name="27117"></a>
<li>A member of a reference type (<a href="8.doc.html#21831">§8.2</a>, <a href="9.doc.html#32392">§9.2</a>, <a href="10.doc.html#11364">§10.7</a>), one of the following:
<ul>
<a name="20164"></a>
<li>A field, one of the following:
<ul>
<a name="20168"></a>
<li>A field declared in a class type <a href="8.doc.html#40898">(§8.3)</a>
<a name="20172"></a>
<li>A constant field declared in an interface type <a href="9.doc.html#78642">(§9.3)</a>
<a name="20173"></a>
<li>The field <code>length</code>, which is implicitly a member of every array type <a href="10.doc.html#11364">(§10.7)</a>
</ul>
<a name="33764"></a>
<li>A method, one of the following:
<ul>
<a name="38090"></a>
<li>A method (<code>abstract</code> or otherwise) declared in a class type <a href="8.doc.html#40420">(§8.4)</a>
<a name="33944"></a>
<li>A method (always <code>abstract</code>) declared in an interface type <a href="9.doc.html#78651">(§9.4)</a>
</ul>
</ul>
</ul><ul><a name="31380"></a>
<li>A parameter, one of the following:
<ul>
<a name="33773"></a>
<li>A parameter of a method or constructor of a class (<a href="8.doc.html#38698">§8.4.1</a>, <a href="8.doc.html#29488">§8.6.1</a>)
<a name="33775"></a>
<li>A parameter of an <code>abstract</code> method of an interface <a href="9.doc.html#78651">(§9.4)</a>
<a name="31398"></a>
<li>A parameter of an exception handler declared in a <code>catch</code> clause of a <code>try</code> statement <a href="14.doc.html#79311">(§14.18)</a>
</ul>
<a name="31392"></a>
<li>A local variable, one of the following:
<ul>
<a name="33776"></a>
<li>A local variable declared in a block <a href="14.doc.html#5920">(§14.3)</a>
<a name="33777"></a>
<li>A local variable declared in a <code>for</code> statement <a href="14.doc.html#24588">(§14.12)</a>
</ul>
</ul><a name="56358"></a>
Constructors <a href="8.doc.html#41652">(§8.6)</a> are also introduced by declarations, but use the name of the
class in which they are declared rather than introducing a new name.
<p><a name="31692"></a>
<h2>6.2 Names and Identifiers</h2>
<a name="61760"></a>
A <i>name</i> is used to refer to an entity declared in a Java program.
<p><a name="61763"></a>
There are two forms of names: simple names and qualified names. A <i>simple name</i> is a single identifier. A <i>qualified name</i> consists of a name, a "<code>.</code>" token, and an identifier.<p>
<a name="21637"></a>
In determining the meaning of a name <a href="6.doc.html#20569">(§6.5)</a>, the Java language takes into account the context in which the name appears. It distinguishes among contexts where a name must denote (refer to) a package <a href="6.doc.html#22349">(§6.5.3)</a>, a type <a href="6.doc.html#21721">(§6.5.4)</a>, a variable or value in an expression <a href="6.doc.html#21650">(§6.5.5)</a>, or a method <a href="6.doc.html#21652">(§6.5.6)</a>.<p>
<a name="33483"></a>
Not all identifiers in Java programs are a part of a name. Identifiers are also used in the following situations:<p>
<ul><a name="33485"></a>
<li>In declarations <a href="6.doc.html#33757">(§6.1)</a>, where an identifier may occur to specify the name by which the declared entity will be known
<a name="33490"></a>
<li>In field access expressions <a href="15.doc.html#41267">(§15.10)</a>, where an identifier occurs after a "<code>.</code>" token to indicate a member of an object that is the value of an expression or the keyword <code>super</code> that appears before the "<code>.</code>" token
<a name="33491"></a>
<li>In some method invocation expressions <a href="15.doc.html#20448">(§15.11)</a>, where an identifier may occur after a "<code>.</code>" token and before a "<code>(</code>" token to indicate a method to be invoked for an object that is the value of an expression or the keyword <code>super</code> that appears before the "<code>.</code>" token
<a name="33492"></a>
<li>As labels in labeled statements <a href="14.doc.html#78993">(§14.6)</a> and in <code>break</code> <a href="14.doc.html#6842">(§14.13)</a> and <code>continue</code> <a href="14.doc.html#6122">(§14.14)</a> statements that refer to statement labels
</ul><a name="38317"></a>
In the example:
<p><pre><a name="38318"></a>
class Test {
<a name="38319"></a> public static void main(String[] args) {
<a name="31441"></a> Class c = System.out.getClass();
<a name="38320"></a> System.out.println(c.toString().length() +
<a name="31466"></a> args[0].length() + args.length);
<a name="38324"></a> }
<a name="38325"></a>}
</pre><a name="61602"></a>
the identifiers <code>Test</code>, <code>main</code>, and the first occurrences of <code>args</code> and <code>c</code> are not names;
rather, they are used in declarations to specify the names of the declared entities.
The names <code>String</code>, <code>Class</code>, <code>System.out.getClass,</code> <code>System.out.println</code>,
<code>c.toString</code>, <code>args</code>, and <code>args.length</code> appear in the example. The first occurrence
of <code>length</code> is not a name, but rather an identifier appearing in a method invocation
expression <a href="15.doc.html#20448">(§15.11)</a>. The second occurrence of <code>length</code> is not a name, but
rather an identifier appearing in a method invocation expression <a href="15.doc.html#20448">(§15.11)</a>.
<p><a name="61609"></a>
The identifiers used in labeled statements and their associated <code>break</code> and <code>continue</code> statements are completely separate from those used in declarations. Thus, the following code is valid:<p>
<pre><a name="38178"></a>
class TestString {
<br><a name="38277"></a> char[] value;
<br><br><a name="38278"></a> int offset, count;
<br><a name="38262"></a> int indexOf(TestString str, int fromIndex) {
<a name="38179"></a> char[] v1 = value, v2 = str.value;
<a name="38309"></a> int max = offset + (count - str.count);
<a name="31499"></a> int start = offset + ((fromIndex < 0) ? 0 : fromIndex);
<a name="38310"></a> i:
<a name="38312"></a> for (int i = start; i <= max; i++)<br>
{
<a name="38184"></a> int n = str.count, j = i, k = str.offset;
<a name="38187"></a> while (n-- != 0) {
<a name="38188"></a> if (v1[j++] != v2[k++])
<a name="38189"></a> continue i;
<a name="38191"></a> }
<a name="38241"></a> return i - offset;
<a name="38251"></a> }
<a name="38194"></a> return -1;
<a name="38266"></a> }
<a name="38274"></a>}
</pre><a name="54045"></a>
This code was taken from a version of the class <code>String</code> and its method <code>indexOf</code>
<a href="javalang.doc11.html#30903">(§20.12.26)</a>, where the label was originally called <code>test</code>. Changing the label to
have the same name as the local variable <code>i</code> does not hide the label in the scope of
the declaration of <code>i</code>. The identifier <code>max</code> could also have been used as the statement
label; the label would not hide the local variable <code>max</code> within the labeled statement.
<p><a name="33623"></a>
<h2>6.3 Scope of a Simple Name</h2>
<a name="34028"></a>
The <i>scope</i> of a declaration is the region of the program within which the entity
declared by the declaration can be referred to using a simple name:
<p><ul><a name="56405"></a>
<li>The scope of a package, as introduced by a <code>package</code> declaration, is determined by the host system <a href="7.doc.html#13180">(§7.4.3)</a>. All Java code is within the scope of the standard package named <code>java</code>, so the package <code>java</code> can always be referred to by Java code.
<a name="31532"></a>
<li>The scope of a type imported by a single-type-import declaration <a href="7.doc.html#26699">(§7.5.1)</a> or type-import-on-demand declaration <a href="7.doc.html#26725">(§7.5.2)</a> is all the class and interface type declarations <a href="7.doc.html#26783">(§7.6)</a> in the compilation unit in which the import declaration appears.
<a name="56422"></a>
<li>The scope of a type introduced by a class type declaration <a href="8.doc.html#39196">(§8.1.1)</a> or interface type declaration <a href="9.doc.html#39713">(§9.1.1)</a> is the declarations of all class and interface types in all the compilation units <a href="7.doc.html#40031">(§7.3)</a> of the package in which it is declared.
<a name="19824"></a>
<li>The scope of a member declared in or inherited by a class type <a href="8.doc.html#21831">(§8.2)</a> or interface type <a href="9.doc.html#32392">(§9.2)</a> is the entire declaration of the class or interface type. The declaration of a member needs to appear before it is used only when the use is in a field initialization expression (<a href="8.doc.html#24510">§8.3.2</a>, <a href="12.doc.html#44630">§12.4.2</a>, <a href="12.doc.html#44670">§12.5</a>). This means that a compile-time error results from the test program:
</ul><pre><a name="19834"></a>
class Test {
<a name="19835"></a> int i = j; // compile-time error: incorrect forward reference
<a name="19836"></a> int j = 1;
<a name="19837"></a> }
</pre><ul><a name="19838"></a>
<br><br>whereas the following example compiles without error:
</ul><pre><a name="19839"></a>
class Test {
<a name="19840"></a> Test() { k = 2; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -