简体   繁体   English

Java 7规范:变量标识符是名称,但字段名称不是? 为什么?

[英]Java 7 specification: variable identifiers are names, but field names are not? Why?

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2

Specification says: 规格说:

In determining the meaning of a name (§6.5), the context in which the name appears is taken into account. 在确定名称的含义(第6.5节)时,会考虑名称出现的上下文。 The rules of §6.5 distinguish among contexts where a name must denote (refer to) a package (§6.5.3), a type (§6.5.5), a variable or value in an expression (§6.5.6), or a method (§6.5.7). §6.5的规则区分名称必须表示(引用)包(第6.5.3节),类型(第6.5.5节),表达式中的变量或值(第6.5.6节)的上下文,或者方法(第6.5.7节)。

So, as far as I understand, names can only denote the above entities - which do not contain fields . 所以,据我所知,名称只能表示上述实体 - 不包含字段

This seems to be in line with the statements made a bit below: 这似乎与下面的陈述一致:

Not all identifiers in a program are a part of a name. 并非程序中的所有标识符都是名称的一部分。 Identifiers are also used in the following situations: 标识符也用于以下情况:

  • ... ...

  • In field access expressions (§15.11), where an identifier occurs after a "." 在字段访问表达式(第15.11节)中,标识符出现在“。”之后。 token to indicate a member of an object that is the value of an expression or the keyword super that appears before the "." token表示对象的成员,该对象是表达式的值或在“。”之前出现的关键字super。 token 代币

  • ... ...

So, my question is: what is the reason for this distinction between local variables and fields? 所以,我的问题是:局部变量和字段之间区别的原因是什么?

The important part is: 重要的是:

The rules of §6.5 distinguish among contexts where a name must denote (refer to) a package (§6.5.3), a type (§6.5.5), a variable or value in an expression (§6.5.6) , or a method (§6.5.7). §6.5的规则区分名称必须表示(引用)包(第6.5.3节),类型(第6.5.5节), 表达式中的变量或值(第6.5.6节)的上下文 ,或者方法(第6.5.7节)。

A reference to a field is also a name in this context because it is a value in an expression. 对字段的引用在此上下文中也是 名称 ,因为它是表达式中的值。 (§6.5.6) (§6.5.6)

There are many points at which this is explained in §6.5.6, I've highlighted a few in bold below: 有许多要点在§6.5.6中对此进行了解释,我在下面突出显示了一些粗体:

6.5.6.1. 6.5.6.1。 Simple Expression Names 简单的表达式名称

If an expression name consists of a single Identifier, then there must be exactly one declaration denoting either a local variable, parameter, or field visible (§6.4.1) at the point at which the Identifier occurs. 如果表达式名称由单个标识符组成,那么在标识符出现的位置必须只有一个声明表示局部变量,参数或字段可见(第6.4.1节) Otherwise, a compile-time error occurs. 否则,发生编译时错误。

If the declaration denotes an instance variable (§8.3) , the expression name must appear within the declaration of an instance method (§8.4), constructor (§8.8), instance initializer (§8.6), or instance variable initializer (§8.3.2.2). 如果声明表示实例变量(第8.3节) ,则表达式名称必须出现在实例方法(第8.4节),构造函数(第8.8节),实例初始化程序(第8.6节)或实例变量初始化程序(第8.3节)的声明中。 2.2)。

[...] [...]

6.5.6.2. 6.5.6.2。 Qualified Expression Names 合格的表达名称

If an expression name is of the form Q.Id, then Q has already been classified as a package name, a type name, or an expression name . 如果表达式名称的格式为Q.Id,则Q已被分类为包名称,类型名称或表达式名称

[...] [...]

If Q is an expression name, let T be the type of the expression Q: 如果Q是表达式名称,则让T为表达式Q的类型:

If T is not a reference type, a compile-time error occurs. 如果T不是引用类型,则发生编译时错误。

If there is not exactly one accessible (§6.6) member of the type T that is a field named Id , then a compile-time error occurs. 如果类型T中没有一个可访问的(§6.6)成员是名为Id字段 ,则会发生编译时错误。

[etc. [等等。 etc.] 等等。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM