CHAPTER 6: Names Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

6.5 Determining the Meaning of a Name

6.5.1 Syntactic Classification of a Name According to Context , 6.5.2 Reclassification of Contextually Ambiguous Names , 6.5.3 Meaning of Package Names , 6.5.4 Meaning of Type Names , 6.5.5 Meaning of Expression Names , 6.5.6 Meaning of Method Names

The meaning of a name in Java depends on the context in which it is used. The determination of the meaning of a name requires three steps. First, context causes a name syntactically to fall into one of five categories: PackageName, TypeName, ExpressionName, MethodName, or AmbiguousName. Second, a name that is initially classified by its context as an AmbiguousName is then reclassified by certain scoping rules to be a PackageName, TypeName, or ExpressionName. Third, the resulting category then dictates the final determination of the meaning of the name (or a compilation error if the name has no meaning).


PackageName:

	Identifier

	PackageName . Identifier

TypeName:

	Identifier

	PackageName . Identifier

ExpressionName:

	Identifier

	AmbiguousName . Identifier

MethodName:

	Identifier

	AmbiguousName . Identifier

AmbiguousName:

	Identifier

	AmbiguousName . Identifier

Java's use of context helps to minimize name conflicts between entities of different kinds. Such conflicts will be rare if the naming conventions described in S6.8 are followed. Nevertheless, conflicts may arise unintentionally as types developed by different programmers or different organizations evolve. For example, types, methods, and fields may have the same name. Java never has trouble distinguishing between a method and a field with the same name, since the context of a use always tells whether a method or a field is intended.


6.5.1 Syntactic Classification of a Name According to Context

A name is syntactically classified as a PackageName in these contexts:

A name is syntactically classified as a TypeName in these contexts:

A name is syntactically classified as an ExpressionName in these contexts:

A name is syntactically classified as a MethodName in this context:

· Before the "( " in a method invocation expression (S15.11)

A name is syntactically classified as an AmbiguousName in these contexts:


6.5.2 Reclassification of Contextually Ambiguous Names

An AmbiguousName is then reclassified as follows:

As an example, consider the following contrived "library code":


package ORG.rpgpoet;


import java.util.Random;

interface Music { Random[] wizards = new Random[4]; }

and then consider this example code in another package:


package bazola;



class Gabriel {
	static int n = ORG.rpgpoet.Music.wizards.length;
}

First of all, the name ORG.rpgpoet.Music.wizards.length is classified as an ExpressionName because it functions as a PostfixExpression. Therefore, each of the names:


ORG.rpgpoet.Music.wizards
ORG.rpgpoet.Music
ORG.rpgpoet
ORG

is initially classified as an AmbiguousName. These are then reclassified:


6.5.3 Meaning of Package Names

The meaning of a name classified as a PackageName is determined as follows.

6.5.3.1 Simple Package Names

If a package name consists of a single Identifier, 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 (S7.4.3), then a compile-time error occurs.

6.5.3.2 Qualified Package Names

If a package name is of the form Q. Id, then Q must also be a package name. The package name Q. Id names a package that is the member named Id within the package named by Q. If Q does not name an accessible package or Id does not name an accessible subpackage of that package, then a compile-time error occurs.


6.5.4 Meaning of Type Names

The meaning of a name classified as a TypeName is determined as follows.

6.5.4.1 Simple Type Names

If a type name consists of a single Identifier, 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:

This order for considering type declarations is designed to choose the most explicit of two or more applicable type declarations.

6.5.4.2 Qualified Type Names

If a type name is of the form Q. Id, then Q must be a package name. The type name Q. Id names a type that is the member named Id within the package named by Q. If Q does not name an accessible package, or Id does not name a type within that package, or the type named Id within that package is not accessible (S6.6), then a compile-time error occurs.

The example:

package wnj.test;


class Test {
	public static void main(String[] args) {
		java.util.Date date =
			new java.util.Date(System.currentTimeMillis());
		System.out.println(date.toLocaleString());
	}
}

produced the following output the first time it was run:

Sun Jan 21 22:56:29 1996

In this example:


6.5.5 Meaning of Expression Names

The meaning of a name classified as an ExpressionName is determined as follows.

6.5.5.1 Simple Expression Names

If an expression name consists of a single Identifier, then:

If the field is an instance variable (S8.3.1.1), the expression name must appear within the declaration of an instance method (S8.4), constructor (S8.6), or instance variable initializer (S8.3.2.2). If it appears within a static method (S8.4.3.2), static initializer (S8.5), or initializer for a static variable (S8.3.1.1, S12.4.2), then a compile-time error occurs.

In the example:


class Test {

	static int v;


	static final int f = 3;


	public static void main(String[] args) {
		int i;
		i = 1;
		v = 2;
		f = 33;										// compile-time error
		System.out.println(i + " " + v + " " + f);
	}

}

the names used as the left-hand-sides in the assignments to i , v , and f denote the local variable i , the field v , and the value of f (not the variable f , because f is a final 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:

1 2 3


6.5.5.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:

The example:


class Point {
	int x, y;
	static int nPoints;
}

class Test {
	public static void main(String[] args) {
		int i = 0;
		i.x++;								// compile-time error
		Point p = new Point();
		p.nPoints();								// compile-time error
	}
}

encounters two compile-time errors, because the int variable i has no members, and because nPoints is not a method of class Point .


6.5.6 Meaning of Method Names

A MethodName can appear only in a method invocation expression (S15.11). The meaning of a name classified as a MethodName is determined as follows.

6.5.6.1 Simple Method Names

If a method name consists of a single Identifier, then Identifier is the method name to be used for method invocation. The Identifier must name at least one method of the class or interface within whose declaration the Identifier appears. See S15.11 for further discussion of the interpretation of simple method names in method invocation expressions.

6.5.6.2 Qualified Method Names

If a method name is of the form Q. Id, then Q has already been classified as a package name, a type name, or an expression name. If Q is a package name, then a compile-time error occurs. Otherwise, Id is the method name to be used for method invocation. If Q is a type name, then Id must name at least one static method of the type Q. If Q is an expression name, then let T be the type of the expression Q; Id must name at least one method of the type T. See S15.11 for further discussion of the interpretation of qualified method names in method invocation expressions.

Top© 1996 Sun Microsystems, Inc. All rights reserved.