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

📄 6.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<li>If there is not exactly one member of that class <a href="8.doc.html#21831">(&#167;8.2)</a> that is a field with that name, then a compile-time error results.
<a name="22063"></a>
<li>Otherwise, if the single member field with that name is declared <code>final</code> <a href="8.doc.html#35962">(&#167;8.3.1.2)</a>, then the expression name denotes the value of the field. The type of the expression name is the declared type of the field. If the <i>Identifier</i> appears in a context that requires a variable and not a value, then a compile-time error occurs.
<a name="22077"></a>
<li>Otherwise, the expression name denotes a variable, the single member field with that name. The type of the expression name is the field's declared type.
</ul>
</ul><ul><a name="60351"></a>
<br><br>If the field is an instance variable <a href="8.doc.html#37544">(&#167;8.3.1.1)</a>, the expression name must appear within the declaration of an instance method <a href="8.doc.html#40420">(&#167;8.4)</a>, constructor <a href="8.doc.html#41652">(&#167;8.6)</a>, or instance variable initializer <a href="8.doc.html#38013">(&#167;8.3.2.2)</a>. If it appears within a <code>static</code> method <a href="8.doc.html#229128">(&#167;8.4.3.2)</a>, static initializer <a href="8.doc.html#39245">(&#167;8.5)</a>, or initializer for a <code>static</code> variable (<a href="8.doc.html#37544">&#167;8.3.1.1</a>, <a href="12.doc.html#44630">&#167;12.4.2</a>), then a compile-time error occurs.
</ul><ul><a name="60376"></a>
<li>Otherwise, the identifier appears within an interface declaration <a href="9.doc.html#238678">(&#167;9)</a>:
<ul>
<a name="60380"></a>
<li>If there is not exactly one member of that interface <a href="9.doc.html#32392">(&#167;9.2)</a> that is a field with that name, then a compile-time error results.
<a name="32963"></a>
<li>Otherwise, the expression name denotes the value of the single member field of that name. The type of the expression name is the declared type of the field. If the <i>Identifier</i> appears in a context that requires a variable and not a value, then a compile-time error occurs.
</ul>
</ul><a name="22118"></a>
In the example:
<p><pre><a name="22119"></a>
class Test {
<br><a name="22120"></a>	static int v;
<br><br><a name="61612"></a>	static final int f = 3;
<br><a name="61613"></a>
	public static void main(String[] args) {
<a name="61614"></a>		int i;
<a name="22124"></a>		i = 1;
<a name="22125"></a>		v = 2;
<a name="22126"></a>		f = 33;										// compile-time error
<a name="22127"></a>		System.out.println(i + " " + v + " " + f);
<a name="22128"></a>	}
<br><a name="22129"></a>}
</pre><a name="22130"></a>
the names used as the left-hand-sides in the assignments to <code>i</code>, <code>v</code>, and <code>f</code> denote the 
local variable <code>i</code>, the field <code>v</code>, and the value of <code>f</code> (not the variable <code>f</code>, because <code>f</code> is a 
<code>final</code> variable). The example therefore produces an error at compile time 
because the last assignment does not have a variable as its left-hand side. If the 
erroneous assignment is removed, the modified code can be compiled and it will 
produce the output:
<p><pre><a name="22131"></a>1 2 3
</pre><a name="22133"></a>
<h4>6.5.5.2    Qualified Expression Names</h4>
<a name="33085"></a>
If an expression name is of the form <i>Q</i><code>.</code><i>Id</i>, then <i>Q</i> has already been classified as a 
package name, a type name, or an expression name:
<p><ul><a name="22747"></a>
<li>If <i>Q</i> is a package name, then a compile-time error occurs.
<a name="22294"></a>
<li>If <i>Q</i> is a type name that names a class type <a href="8.doc.html#3857">(&#167;8)</a>, then:
<ul>
<a name="22298"></a>
<li>If there is not exactly one accessible <a href="6.doc.html#33916">(&#167;6.6)</a> member of the class type that is a field named <i>Id</i>, then a compile-time error occurs.
<a name="22299"></a>
<li>Otherwise, if the single accessible member field is not a class variable (that is, it is not declared <code>static</code>), then a compile-time error occurs.
<a name="41289"></a>
<li>Otherwise, if the class variable is declared <code>final</code>, then <i>Q</i><code>.</code><i>Id</i> denotes the value of the class variable. The type of the expression <i>Q</i><code>.</code><i>Id</i> is the declared type of the class variable. If <i>Q</i><code>.</code><i>Id</i> appears in a context that requires a variable and not a value, then a compile-time error occurs.
<a name="22303"></a>
<li>Otherwise, <i>Q</i><code>.</code><i>Id</i> denotes the class variable. The type of the expression <i>Q</i><code>.</code><i>Id</i> is the declared type of the class variable.
</ul>
<a name="22145"></a>
<li>If <i>Q</i> is a type name that names an interface type <a href="9.doc.html#238678">(&#167;9)</a>, then:
<ul>
<a name="33167"></a>
<li>If there is not exactly one accessible <a href="6.doc.html#33916">(&#167;6.6)</a> member of the interface type that is a field named <i>Id</i>, then a compile-time error occurs.
<a name="33169"></a>
<li>Otherwise, <i>Q</i><code>.</code><i>Id</i> denotes the value of the field. The type of the expression <i>Q</i><code>.</code><i>Id</i> is the declared type of the field. If <i>Q</i><code>.</code><i>Id</i> appears in a context that requires a variable and not a value, then a compile-time error occurs.
</ul>
<a name="22165"></a>
<li>If <i>Q</i> is an expression name, let <i>T</i> be the type of the expression <i>Q</i>:
<ul>
<a name="22169"></a>
<li>If <i>T</i> is not a reference type, a compile-time error occurs.
<a name="33185"></a>
<li>If there is not exactly one accessible <a href="6.doc.html#33916">(&#167;6.6)</a> member of the type <i>T</i> that is a field named <i>Id</i>, then a compile-time error occurs.
<a name="22170"></a>
<li>Otherwise, if this field is any of the following:
<ul>
<a name="33204"></a>
<li>A field of an interface type
<a name="33205"></a>
<li>A <code>final</code> field of a class type (which may be either a class variable or an instance variable)
<a name="33208"></a>
<li>The <code>final</code> field <code>length</code> of an array type
</ul>
<a name="33211"></a>
<li>then <i>Q</i><code>.</code><i>Id</i> denotes the value of the field. The type of the expression <i>Q</i><code>.</code><i>Id</i> is the declared type of the field. If <i>Q</i><code>.</code><i>Id</i> appears in a context that requires a variable and not a value, then a compile-time error occurs.
<a name="22173"></a>
<li>Otherwise, <i>Q</i><code>.</code><i>Id</i> denotes a variable, the field <i>Id</i> of class <i>T</i>, which may be either a class variable or an instance variable. The type of the expression <i>Q</i><code>.</code><i>Id</i> is the declared type of the field
</ul>
</ul><a name="62391"></a>
The example:
<p><pre><a name="22175"></a>
class Point {
<a name="22176"></a>	int x, y;
<a name="22177"></a>	static int nPoints;
<a name="22178"></a>}
<a name="22179"></a>
class Test {
<a name="22180"></a>	public static void main(String[] args) {
<a name="22181"></a>		int i = 0;
<a name="22182"></a>		i.x++;								// compile-time error
<a name="22183"></a>		Point p = new Point();
<a name="22184"></a>		p.nPoints();								// compile-time error
<a name="22185"></a>	}
<a name="22186"></a>}
</pre><a name="22187"></a>
encounters two compile-time errors, because the <code>int</code> variable <code>i</code> has no members, 
and because <code>nPoints</code> is not a method of class <code>Point</code>.
<p><a name="21652"></a>
<h3>6.5.6    Meaning of Method Names</h3>
<a name="32771"></a>
A <i>MethodName</i> can appear only in a method invocation expression <a href="15.doc.html#20448">(&#167;15.11)</a>. The 
meaning of a name classified as a <i>MethodName</i> is determined as follows.
<p><a name="54512"></a>
<h4>6.5.6.1    Simple Method Names</h4>
<a name="54513"></a>
If a method name consists of a single <i>Identifier</i>, then <i>Identifier</i> is the method name 
to be used for method invocation. The <i>Identifier</i> must name at least one method of 
the class or interface within whose declaration the <i>Identifier</i> appears. See <a href="15.doc.html#20448">&#167;15.11</a> 
for further discussion of the interpretation of simple method names in method 
invocation expressions.
<p><a name="54547"></a>
<h4>6.5.6.2    Qualified Method Names</h4>
<a name="33243"></a>
If a method name is of the form <i>Q</i><code>.</code><i>Id</i>, then <i>Q</i> has already been classified as a 
package name, a type name, or an expression name. If <i>Q</i> is a package name, then a 
compile-time error occurs. Otherwise, <i>Id</i> is the method name to be used for 
method invocation. If <i>Q</i> is a type name, then <i>Id</i> must name at least one <code>static</code> 
method of the type <i>Q</i>. If <i>Q</i> is an expression name, then let <i>T</i> be the type of the 
expression <i>Q</i>; <i>Id</i> must name at least one method of the type <i>T</i>. See <a href="15.doc.html#20448">&#167;15.11</a> for further
discussion of the interpretation of qualified method names in method invocation
expressions.
<p><a name="33916"></a>
<h2>6.6    Qualified Names and Access Control</h2>
<a name="33917"></a>
Qualified names are a means of access to members of packages and reference 
types; related means of access include field access expressions <a href="15.doc.html#41267">(&#167;15.10)</a> and 
method invocation expressions <a href="15.doc.html#20448">(&#167;15.11)</a>. All three are syntactically similar in that 
a "<code>.</code>" token appears, preceded by some indication of a package, type, or expression
having a type and followed by an <i>Identifier</i> that names a member of the package
or type. These are collectively known as constructs for <i>qualified access</i>.
<p><a name="33924"></a>
Java provides mechanisms for <i>access control</i>, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. Access control applies to qualified access and to the invocation of constructors by class instance creation expressions <a href="15.doc.html#41147">(&#167;15.8)</a>, explicit constructor invocations <a href="8.doc.html#78435">(&#167;8.6.5)</a>, and the method <code>newInstance</code> of class <code>Class</code> <a href="javalang.doc2.html#15088">(&#167;20.3.6)</a>.<p>
<a name="62634"></a>
If access is permitted, then the accessed entity is said to be <i>accessible</i>.<p>
<a name="62395"></a>
<h3>6.6.1    Determining Accessibility</h3>
<ul><a name="33934"></a>
<li>Whether a package is accessible is determined by the host system <a href="7.doc.html#37758">(&#167;7.2)</a>.
<a name="62409"></a>
<li>If a class or interface type is declared <code>public</code>, then it may be accessed by any Java code that can access the package in which it is declared. If a class or interface type is not declared <code>public</code>, then it may be accessed only from within the package in which it is declared.
<a name="62410"></a>
<li>A member (field or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
<ul>
<a name="62795"></a>
<li>If the member or constructor is declared <code>public</code>, then access is permitted. All members of interfaces are implicitly <code>public</code>.
<a name="62561"></a>
<li>Otherwise, if the member or constructor is declared <code>protected</code>, then access is permitted only when one of the following is true:
<ul><ul>
<a name="62562"></a>
<li>Access to the member or constructor occurs from within the package containing the class in which the <code>protected</code> member is declared.
<a name="62452"></a>
<li>Access occurs within a subclass of the class in which the <code>protected</code> member is declared, and the access is correct as described in <a href="6.doc.html#62587">&#167;6.6.2</a>.
</ul>
</ul><a name="62857"></a>
<li>Otherwise, if the member or constructor is declared <code>private</code>, then access is permitted only when it occurs from within the class in which it is declared.
<a name="62586"></a>
<li>Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.
</ul>
</ul><a name="62587"></a>
<h3>6.6.2    Details on <code>protected</code> Access</h3>
<a name="62638"></a>
A <code>protected</code> member or constructor of an object may be accessed from outside 
the package in which it is declared only by code that is responsible for the implementation
of that object. Let <i>C</i> be the class in which a <code>protected</code> member or constructor
is declared and let <i>S</i> be the subclass of <i>C</i> in whose declaration the use of 
the <code>protected</code> member or constructor occurs. Then:
<p><ul><a name="60602"></a>
<li>If an access is of a <code>protected</code> member (field or method), let <i>Id</i> be its name. Consider then the means of qualified access:
<ul>
<a name="33943"></a>
<li>If the access

⌨️ 快捷键说明

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