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

📄 6.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="19841"></a>		int j = 1;
<a name="19842"></a>		int i = j;
<a name="19843"></a>		int k;
<a name="19844"></a>	}
</pre><ul><a name="19848"></a>
<br><br>even though the constructor <a href="8.doc.html#41652">(&#167;8.6)</a> for <code>Test</code> refers to the field <code>k</code> that is declared three lines later.
</ul><ul><a name="31556"></a>
<li>The scope of a parameter of a method <a href="8.doc.html#38698">(&#167;8.4.1)</a> is the entire body of the method.
</ul><ul><a name="31566"></a>
<li>The scope of a parameter of a constructor <a href="8.doc.html#29488">(&#167;8.6.1)</a> is the entire body of the constructor.
<a name="19941"></a>
<li>The scope of a local variable declaration in a block <a href="14.doc.html#32644">(&#167;14.3.2)</a> is the rest of the block in which the declaration appears, starting with its own initializer <a href="14.doc.html#5920">(&#167;14.3)</a> and including any further declarators to the right in the local variable declaration statement.
<a name="56450"></a>
<li>The scope of a local variable declared in the <i>ForInit</i> part of a <code>for</code> statement <a href="14.doc.html#24588">(&#167;14.12)</a> includes all of the following:
<ul>
<a name="23092"></a>
<li>Its own initializer
<a name="56451"></a>
<li>Any further declarators to the right in the <i>ForInit</i> part of the <code>for</code> statement
<a name="56452"></a>
<li>The <i>Expression</i> and <i>ForUpdate</i> parts of the <code>for</code> statement
<a name="56453"></a>
<li>The contained <i>Statement</i>
</ul>
<a name="56454"></a>
<li>The scope of a parameter of an exception handler that is declared in a <code>catch</code> clause of a <code>try</code> statement <a href="14.doc.html#79311">(&#167;14.18)</a> is the entire block associated with the <code>catch</code>.
</ul><a name="56458"></a>
These rules imply that declarations of class and interface types need not appear 
before uses of the types.
<p><a name="20278"></a>
In the example:<p>
<pre><a name="56459"></a>package points;
</pre><pre><a name="56460"></a>
class Point {
<a name="56461"></a>	int x, y;
<a name="56462"></a>	PointList list;
<a name="56463"></a>	Point next;
<a name="56464"></a>}
<a name="56465"></a>
class PointList {
<a name="56466"></a>	Point first;
<a name="56468"></a>}
</pre><a name="34047"></a>
the use of <code>PointList</code> in class <code>Point</code> is correct, because the scope of the class type 
name <code>PointList</code> includes both class <code>Point</code> and class <code>PointList</code>, as well as any 
other type declarations in other compilation units of package <code>points</code>.
<p><a name="34133"></a>
<h3>6.3.1    Hiding Names</h3>
<a name="34051"></a>
Some declarations may be hidden <a href="6.doc.html#34133">(&#167;6.3.1)</a> in part of their scope by another declaration
of the same name, in which case a simple name cannot be used to refer to 
the declared entity.
<p><a name="62323"></a>
The example:<p>
<pre><a name="34052"></a>
class Test {
<a name="34053"></a>	static int x = 1;
<a name="34054"></a>	public static void main(String[] args) {
<a name="34055"></a>		int x = 0;
<a name="34056"></a>		System.out.print("x=" + x);
<a name="34057"></a>		System.out.println(", Test.x=" + Test.x);
<a name="34058"></a>	}
<a name="34059"></a>}
</pre><a name="34060"></a>
produces the output:
<p><pre><a name="34061"></a>x=0, Test.x=1
</pre><a name="34062"></a>
This example declares:
<p><ul><a name="62327"></a>
<li>a class <code>Test</code>
<a name="62331"></a>
<li>a class (<code>static</code>) variable <code>x</code> that is a member of the class <code>Test</code>
<a name="62332"></a>
<li>a class method <code>main</code> that is a member of the class <code>Test</code>
<a name="62333"></a>
<li>a parameter <code>args</code> of the <code>main</code> method
<a name="62334"></a>
<li>a local variable <code>x</code> of the <code>main</code> method
</ul><a name="34066"></a>
Since the scope of a class variable includes the entire body of the class <a href="8.doc.html#21831">(&#167;8.2)</a> the class variable <code>x</code> would normally be available throughout the entire body of the method <code>main</code>. In this example, however, the class variable <code>x</code> is hidden within the body of the method <code>main</code> by the declaration of the local variable <code>x</code>.<p>
<a name="62312"></a>
A local variable has as its scope the rest of the block in which it is declared <a href="14.doc.html#32644">(&#167;14.3.2)</a>; in this case this is the rest of the body of the <code>main</code> method, namely its initializer "<code>0</code>" and the invocations of <code>print</code> and <code>println</code>.<p>
<a name="62314"></a>
This means that:<p>
<ul><a name="62313"></a>
<li>The expression "<code>x</code>" in the invocation of <code>print</code> refers to (denotes) the value of the local variable <code>x</code>.
<a name="62320"></a>
<li>The invocation of <code>println</code> uses a qualified name <a href="6.doc.html#33916">(&#167;6.6)</a> <code>Test.x</code>, which uses the class type name <code>Test</code> to access the class variable <code>x</code>, because the declaration of <code>Test.x</code> is hidden at this point and cannot be referred to by its simple name.
</ul><a name="62307"></a>
If the standard naming conventions <a href="6.doc.html#11186">(&#167;6.8)</a> are followed, then hiding that would make the identification of separate naming contexts matter should be rare. The following contrived example involves hiding because it does not follow the standard naming conventions:<p>
<pre><a name="34080"></a>
class Point { int x, y; }
<a name="34083"></a>
class Test {
<a name="34084"></a>
	static Point Point(int x, int y) {
<a name="34085"></a>		Point p = new Point();
<a name="34086"></a>		p.x = x; p.y = y;
<a name="34087"></a>		return p;
<a name="34088"></a>	}
<br><a name="34089"></a>
	public static void main(String[] args) {
<a name="34090"></a>		int Point;
<a name="34091"></a>		Point[] pa = new Point[2];
<a name="34092"></a>		for (Point = 0; Point &lt; 2; Point++) {
<a name="34093"></a>			pa[Point] = new Point();
<a name="34094"></a>			pa[Point].x = Point;
<a name="34095"></a>			pa[Point].y = Point;
<a name="34096"></a>		}
<a name="34097"></a>		System.out.println(pa[0].x + "," + pa[0].y);
<a name="34098"></a>		System.out.println(pa[1].x + "," + pa[1].y);
<a name="34099"></a>		Point p = Point(3, 4);
<a name="34100"></a>		System.out.println(p.x + "," + p.y);
<a name="34101"></a>	}
<br><a name="34102"></a>}
</pre><a name="34103"></a>
This compiles without error and executes to produce the output:
<p><pre><a name="34104"></a>
0,0
<a name="34105"></a>1,1<br>
3,4
</pre><a name="34106"></a>
Within the body of <code>main</code>, the lookups of <code>Point</code> find different declarations depending
on the context of the use:
<p><ul><a name="34107"></a>
<li>In the expression "<code>new</code> <code>Point[2]</code>", the two occurrences of the class instance creation expression "<code>new</code> <code>Point()</code>", and at the start of three different local variable declaration statements, the <code>Point</code> is a <i>TypeName</i> <a href="6.doc.html#21721">(&#167;6.5.4)</a> and denotes the class type <code>Point</code> in each case.
<a name="34268"></a>
<li>In the method invocation expression "<code>Point(3,</code> <code>4)</code>" the occurrence of <code>Point</code> is a <i>MethodName </i><a href="6.doc.html#21652">(&#167;6.5.6)</a> and denotes the class (<code>static</code>) method <code>Point</code>.
<a name="34114"></a>
<li>All other names are <i>ExpressionName</i>s <a href="6.doc.html#21650">(&#167;6.5.5)</a> and refer to the local variable <code>Point</code>.
</ul><a name="34119"></a>
The example:
<p><pre><a name="34120"></a>import java.util.*;
</pre><pre><a name="34121"></a>
class Vector {
<a name="34122"></a>	int val[] = { 1 , 2 };
<a name="34123"></a>}
<br><a name="34124"></a>
class Test {
<a name="34125"></a>	public static void main(String[] args) {
<a name="34126"></a>		Vector v = new Vector();
<a name="34127"></a>		System.out.println(v.val[0]);
<a name="34128"></a>	}
<a name="34129"></a>}
</pre><a name="34130"></a>
compiles and prints:
<p><pre><a name="34131"></a>1
</pre><a name="56469"></a>
using the class <code>Vector</code> declared here in preference to class <code>java.util.Vector</code> 
that might be imported on demand.
<p><a name="31814"></a>
<h2>6.4    Members and Inheritance</h2>
<a name="23209"></a>
Packages and reference types have <i>members</i>. The members of a package <a href="7.doc.html#34412">(&#167;7)</a> are 
subpackages <a href="7.doc.html#26535">(&#167;7.1)</a> and all the class <a href="8.doc.html#3857">(&#167;8)</a> and interface <a href="9.doc.html#238678">(&#167;9)</a> types declared in all 
the compilation units <a href="7.doc.html#40031">(&#167;7.3)</a> of the package. The members of a reference type 
<a href="4.doc.html#9317">(&#167;4.3)</a> are fields (<a href="8.doc.html#40898">&#167;8.3</a>, <a href="9.doc.html#78642">&#167;9.3</a>, <a href="10.doc.html#11364">&#167;10.7</a>) and methods (<a href="8.doc.html#40420">&#167;8.4</a>, <a href="9.doc.html#78651">&#167;9.4</a>). Members are either 
declared in the type, or <i>inherited </i>because they are accessible members of a superclass
or superinterface which are neither hidden nor overridden <a href="8.doc.html#228745">(&#167;8.4.6)</a>.
<p><a name="56531"></a>
This section provides an overview of the members of packages and reference types here, as background for the discussion of qualified names and the determination of the meaning of names. For a complete description of membership, see <a href="7.doc.html#26535">&#167;7.1</a>, <a href="8.doc.html#21831">&#167;8.2</a>, <a href="9.doc.html#32392">&#167;9.2</a>, and <a href="10.doc.html#11364">&#167;10.7</a>.<p>
<a name="34993"></a>
<h3>6.4.1    The Members of a Package</h3>
<a name="23133"></a>
A member of a package <a href="7.doc.html#34412">(&#167;7)</a> is a subpackage <a href="7.doc.html#26535">(&#167;7.1)</a>, or a class <a href="8.doc.html#3857">(&#167;8)</a> or interface 
<a href="9.doc.html#238678">(&#167;9)</a> type declared in a compilation unit <a href="7.doc.html#40031">(&#167;7.3)</a> of the package.
<p><a name="56555"></a>
In general, the subpackages of a package are determined by the host system <a href="7.doc.html#37758">(&#167;7.2)</a>. However, the standard package <code>java</code> always includes the subpackages <code>lang</code>, <code>util</code>, <code>io</code>, and <code>net</code> and may include other subpackages. No two distinct members of the same package may have the same simple name <a href="7.doc.html#26535">(&#167;7.1)</a>, but members of different packages may have the same simple name. For example, it is possible to declare a package:<p>
<pre><a name="34982"></a>package vector;
<a name="34983"></a>public class Vector { Object[] vec; }
</pre><a name="34986"></a>
that has as a member a <code>public</code> class named <code>Vector</code>, even though the standard 
package <code>java.util</code> also declares a class named <code>Vector</code>. These two class types 
are different, reflected by the fact that they have different fully qualified names 
<a href="6.doc.html#25430">(&#167;6.7)</a>. The fully qualified name of this example <code>Vector</code> is <code>vector.Vector</code>, 
whereas <code>java.util.Vector</code> is the fully qualified name of the standard <code>Vector</code> 
class. Because the package <code>vector</code> contains a class named <code>Vector</code>, it cannot also 
have a subpackage named <code>Vector</code>.
<p><a name="34757"></a>
<h3>6.4.2    The Members of a Class Type</h3>
<a name="34765"></a>

⌨️ 快捷键说明

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