Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
4035 lines (3958 sloc) 270 KB
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter&nbsp;6.&nbsp;Names</title>
<link rel="stylesheet" type="text/css" href="../../../javaspec.css">
<meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1">
<link rel="home" href="index.html" title="The Java&reg; Language Specification">
<link rel="up" href="index.html" title="The Java&reg; Language Specification">
<link rel="prev" href="jls-5.html" title="Chapter&nbsp;5.&nbsp;Conversions and Contexts">
<link rel="next" href="jls-7.html" title="Chapter&nbsp;7.&nbsp;Packages">
<link rel="copyright" href="jls-0-front.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:rx="http://www.renderx.com/XSL/Extensions" id="logo"><img src="http://www.oracleimg.com/us/assets/oralogo-small.gif" alt="Oracle Logo"><br><p><a target="_blank" href="http://www.oracle.com/us/technologies/java/">Oracle
Technology Network</a> &gt; <a target="_blank" href="http://docs.oracle.com/javase/">Java SE</a>
&gt; <a href="index.html">Java Language Specification</a></p>
</div>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Chapter&nbsp;6.&nbsp;Names</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="jls-5.html">Prev</a>&nbsp;
</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="jls-7.html">Next</a></td>
</tr>
</table>
<hr>
</div>
<div lang="en" class="chapter" title="Chapter&nbsp;6.&nbsp;Names">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="jls-6"></a>Chapter&nbsp;6.&nbsp;Names
</h2>
</div>
</div>
</div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.1">6.1. Declarations</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.2">6.2. Names and Identifiers</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.3">6.3. Scope of a Declaration</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.4">6.4. Shadowing and Obscuring</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.4.1">6.4.1. Shadowing</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.4.2">6.4.2. Obscuring</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.5">6.5. Determining the Meaning of a Name</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.1">6.5.1. Syntactic Classification of a Name According to Context</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.2">6.5.2. Reclassification of Contextually Ambiguous Names</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.3">6.5.3. Meaning of Package Names</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.3.1">6.5.3.1. Simple Package Names</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.3.2">6.5.3.2. Qualified Package Names</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.5.4">6.5.4. Meaning of <span class="emphasis"><em>PackageOrTypeNames</em></span></a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.4.1">6.5.4.1. Simple <span class="emphasis"><em>PackageOrTypeNames</em></span></a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.4.2">6.5.4.2. Qualified <span class="emphasis"><em>PackageOrTypeNames</em></span></a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.5.5">6.5.5. Meaning of Type Names</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.5.1">6.5.5.1. Simple Type Names</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.5.2">6.5.5.2. Qualified Type Names</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.5.6">6.5.6. Meaning of Expression Names</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.6.1">6.5.6.1. Simple Expression Names</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.5.6.2">6.5.6.2. Qualified Expression Names</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.5.7">6.5.7. Meaning of Method Names</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.5.7.1">6.5.7.1. Simple Method Names</a></span></dt>
</dl>
</dd>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.6">6.6. Access Control</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.6.1">6.6.1. Determining Accessibility</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.6.2">6.6.2. Details on protected Access</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-6.html#jls-6.6.2.1">6.6.2.1. Access to a <code class="literal">protected</code> Member</a></span></dt>
<dt><span class="section"><a href="jls-6.html#jls-6.6.2.2">6.6.2.2. Qualified Access to a <code class="literal">protected</code> Constructor</a></span></dt>
</dl>
</dd>
</dl>
</dd>
<dt><span class="section"><a href="jls-6.html#jls-6.7">6.7. Fully Qualified Names and Canonical Names</a></span></dt>
</dl>
</div>
<p class="norm"><a name="jls-6-100"></a>Names are used to refer to
entities declared in a program.
</p>
<p class="norm"><a name="jls-6-110"></a>A declared entity
(<a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a>) is a package, class type (normal or enum),
interface type (normal or annotation type), member (class, interface,
field, or method) of a reference type, type parameter (of a class,
interface, method or constructor), parameter (to a method,
constructor, or exception handler), or local variable.
</p>
<p class="norm"><a name="jls-6-120"></a>Names in programs are
either <span class="emphasis"><em>simple</em></span>, consisting of a single identifier,
or <span class="emphasis"><em>qualified</em></span>, consisting of a sequence of
identifiers separated by "<code class="literal">.</code>" tokens (<a class="xref" href="jls-6.html#jls-6.2" title="6.2.&nbsp;Names and Identifiers">&sect;6.2</a>).
</p>
<p class="norm"><a name="jls-6-130"></a>Every declaration that introduces
a name has a <span class="emphasis"><em>scope</em></span> (<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>),
which is the part of the program text within which the declared entity
can be referred to by a simple name.
</p>
<p class="norm"><a name="jls-6-140"></a>A qualified
name <code class="varname">N.x</code> may be used to refer to
a <span class="emphasis"><em>member</em></span> of a package or reference
type, where <code class="varname">N</code> is a simple
or qualified name and <code class="varname">x</code> is an
identifier. If <code class="varname">N</code> names a package,
then <code class="varname">x</code> is a member of that package, which is either
a class or interface type or a subpackage. If <code class="varname">N</code>
names a reference type or a variable of a reference type,
then <code class="varname">x</code> names a member of that type, which is either
a class, an interface, a field, or a method.
</p>
<p class="norm"><a name="jls-6-150"></a>In determining the meaning of a
name (<a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a>), the context of the occurrence is
used to disambiguate among packages, types, variables, and methods
with the same name.
</p>
<p class="norm"><a name="jls-6-160"></a>Access control
(<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) can be specified in a class, interface,
method, or field declaration to control
when <span class="emphasis"><em>access</em></span> to a member is allowed. Access is a
different concept from scope. Access specifies the part of the program
text within which the declared entity can be referred to by a
qualified name. Access to a declared entity is also relevant in a
field access expression (<a class="xref" href="jls-15.html#jls-15.11" title="15.11.&nbsp;Field Access Expressions">&sect;15.11</a>), a method
invocation expression in which the method is not specified by a simple
name (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>), a method reference expression
(<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>), or a qualified class instance creation
expression (<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>). In the absence of an access
modifier, most declarations have package access, allowing access
anywhere within the package that contains its declaration; other
possibilities are <code class="literal">public</code>, <code class="literal">protected</code>, and <code class="literal">private</code>.
</p>
<p class="norm"><a name="jls-6-170"></a>Fully qualified and canonical
names (<a class="xref" href="jls-6.html#jls-6.7" title="6.7.&nbsp;Fully Qualified Names and Canonical Names">&sect;6.7</a>) are also discussed in this
chapter.
</p>
<div class="section" title="6.1.&nbsp;Declarations">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-6.1"></a>6.1.&nbsp;Declarations
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-6.1-100"></a>A
<span class="emphasis"><em>declaration</em></span> introduces an entity into a program
and includes an identifier (<a class="xref" href="jls-3.html#jls-3.8" title="3.8.&nbsp;Identifiers">&sect;3.8</a>) that can be
used in a name to refer to this entity.
</p>
<p class="norm-static"><a name="jls-6.1-110"></a>A declared
entity is one of the following:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-A"></a>
A package, declared in a <code class="literal">package</code> declaration
(<a class="xref" href="jls-7.html#jls-7.4" title="7.4.&nbsp;Package Declarations">&sect;7.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-B"></a>
An imported type, declared in a single-type-import declaration
or a type-import-on-demand declaration
(<a class="xref" href="jls-7.html#jls-7.5.1" title="7.5.1.&nbsp;Single-Type-Import Declarations">&sect;7.5.1</a>,
<a class="xref" href="jls-7.html#jls-7.5.2" title="7.5.2.&nbsp;Type-Import-on-Demand Declarations">&sect;7.5.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-C"></a>
An imported <code class="literal">static</code> member, declared in a single-static-import
declaration or a static-import-on-demand declaration
(<a class="xref" href="jls-7.html#jls-7.5.3" title="7.5.3.&nbsp;Single-Static-Import Declarations">&sect;7.5.3</a>,
<a class="xref" href="jls-7.html#jls-7.5.4" title="7.5.4.&nbsp;Static-Import-on-Demand Declarations">&sect;7.5.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-D"></a>
A class, declared in a class type declaration
(<a class="xref" href="jls-8.html#jls-8.1" title="8.1.&nbsp;Class Declarations">&sect;8.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-E"></a>
An interface, declared in an interface type declaration
(<a class="xref" href="jls-9.html#jls-9.1" title="9.1.&nbsp;Interface Declarations">&sect;9.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-F"></a>
A type parameter, declared as part of the declaration of a
generic class, interface, method, or constructor
(<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>, <a class="xref" href="jls-9.html#jls-9.1.2" title="9.1.2.&nbsp;Generic Interfaces and Type Parameters">&sect;9.1.2</a>,
<a class="xref" href="jls-8.html#jls-8.4.4" title="8.4.4.&nbsp;Generic Methods">&sect;8.4.4</a>, <a class="xref" href="jls-8.html#jls-8.8.4" title="8.8.4.&nbsp;Generic Constructors">&sect;8.8.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G"></a>
A member of a reference type (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>,
<a class="xref" href="jls-9.html#jls-9.2" title="9.2.&nbsp;Interface Members">&sect;9.2</a>, <a class="xref" href="jls-8.html#jls-8.9.3" title="8.9.3.&nbsp;Enum Members">&sect;8.9.3</a>,
<a class="xref" href="jls-9.html#jls-9.6" title="9.6.&nbsp;Annotation Types">&sect;9.6</a>, <a class="xref" href="jls-10.html#jls-10.7" title="10.7.&nbsp;Array Members">&sect;10.7</a>), one of
the following:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-A"></a>
A member class (<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>,
<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-B"></a>
A member interface (<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>,
<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-C"></a>
An enum constant (<a class="xref" href="jls-8.html#jls-8.9" title="8.9.&nbsp;Enum Types">&sect;8.9</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-D"></a>
A field, one of the following:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-D-A"></a>
A field declared in a class type or enum type
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>,
<a class="xref" href="jls-8.html#jls-8.9.2" title="8.9.2.&nbsp;Enum Body Declarations">&sect;8.9.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-D-B"></a>
A field declared in an interface type or annotation type
(<a class="xref" href="jls-9.html#jls-9.3" title="9.3.&nbsp;Field (Constant) Declarations">&sect;9.3</a>,
<a class="xref" href="jls-9.html#jls-9.6.1" title="9.6.1.&nbsp;Annotation Type Elements">&sect;9.6.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-D-C"></a>
The field <code class="literal">length</code>, which is implicitly a member of
every array type (<a class="xref" href="jls-10.html#jls-10.7" title="10.7.&nbsp;Array Members">&sect;10.7</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-E"></a>
A method, one of the following:
</p>
<div class="norm">
<ul class="norm" type="square">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-E-A"></a>
A method (<code class="literal">abstract</code> or otherwise) declared in a class
type or enum type (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>,
<a class="xref" href="jls-8.html#jls-8.9.2" title="8.9.2.&nbsp;Enum Body Declarations">&sect;8.9.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-G-E-B"></a>
A method (always <code class="literal">abstract</code>) declared in an interface
type or annotation type (<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>,
<a class="xref" href="jls-9.html#jls-9.6.1" title="9.6.1.&nbsp;Annotation Type Elements">&sect;9.6.1</a>)
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-H"></a>
A parameter, one of the following:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-H-A"></a>
A formal parameter of a method or constructor of a class
type or enum type (<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>,
<a class="xref" href="jls-8.html#jls-8.8.1" title="8.8.1.&nbsp;Formal Parameters">&sect;8.8.1</a>, <a class="xref" href="jls-8.html#jls-8.9.2" title="8.9.2.&nbsp;Enum Body Declarations">&sect;8.9.2</a>),
or of a lambda expression
(<a class="xref" href="jls-15.html#jls-15.27.1" title="15.27.1.&nbsp;Lambda Parameters">&sect;15.27.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-H-B"></a>
A formal parameter of an <code class="literal">abstract</code> method of an interface
type or annotation type (<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>,
<a class="xref" href="jls-9.html#jls-9.6.1" title="9.6.1.&nbsp;Annotation Type Elements">&sect;9.6.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-H-C"></a>
An exception parameter of an exception handler declared in a
<code class="literal">catch</code> clause of a <code class="literal">try</code> statement
(<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>)
</p>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-I"></a>
A local variable, one of the following:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-I-A"></a>
A local variable declared in a block
(<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.1-110-I-B"></a>
A local variable declared in a <code class="literal">for</code> statement
(<a class="xref" href="jls-14.html#jls-14.14" title="14.14.&nbsp;The for Statement">&sect;14.14</a>)
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.1-120"></a>Constructors
(<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</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>
<p class="norm"><a name="jls-6.1-200"></a>The declaration of a type which is not
generic (<code class="literal">class C ...</code>) declares one entity: a
non-generic type (<code class="literal">C</code>). A non-generic type is not a
raw type, despite the syntactic similarity. In contrast, the
declaration of a generic type (<code class="literal">class C&lt;T&gt; ...</code>
or <code class="literal">interface C&lt;T&gt; ...</code>) declares two entities:
a generic type (<code class="literal">C&lt;T&gt;</code>) and a corresponding
non-generic type (<code class="literal">C</code>). In this case, the meaning of
the term <code class="literal">C</code> depends on the context where it
appears:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-6.1-200-A"></a>
If genericity is unimportant, as in the <span class="emphasis"><em>non-generic
contexts</em></span> identified below, the
identifier <code class="literal">C</code> denotes the non-generic
type <code class="literal">C</code>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-200-B"></a>
If genericity is important, as in all contexts from
<a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a> except the non-generic contexts, the
identifier <code class="literal">C</code> denotes either:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm"><a name="jls-6.1-200-B-A"></a>
The raw type <code class="literal">C</code> which is the erasure
(<a class="xref" href="jls-4.html#jls-4.6" title="4.6.&nbsp;Type Erasure">&sect;4.6</a>) of the generic
type <code class="literal">C&lt;T&gt;</code>; or
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-200-B-B"></a>
A parameterized type which is a particular parameterization
(<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>) of the generic
type <code class="literal">C&lt;T&gt;</code>.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="norm"><a name="jls-6.1-210"></a>The 13 non-generic contexts are as
follows:
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-A"></a>
In a single-type-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.1" title="7.5.1.&nbsp;Single-Type-Import Declarations">&sect;7.5.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-B"></a>
To the left of the <code class="literal">.</code> in a single-static-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.3" title="7.5.3.&nbsp;Single-Static-Import Declarations">&sect;7.5.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-C"></a>
To the left of the <code class="literal">.</code> in a static-import-on-demand
declaration (<a class="xref" href="jls-7.html#jls-7.5.4" title="7.5.4.&nbsp;Static-Import-on-Demand Declarations">&sect;7.5.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-D"></a>
To the left of the <code class="literal">(</code> in a constructor declaration
(<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-E"></a>
After the <code class="literal">@</code> sign in an annotation (<a class="xref" href="jls-9.html#jls-9.7" title="9.7.&nbsp;Annotations">&sect;9.7</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-F"></a>
To the left of <code class="literal">.</code><code class="literal">class</code> in a class literal
(<a class="xref" href="jls-15.html#jls-15.8.2" title="15.8.2.&nbsp;Class Literals">&sect;15.8.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-G"></a>
To the left of <code class="literal">.</code><code class="literal">this</code> in a qualified <code class="literal">this</code> expression
(<a class="xref" href="jls-15.html#jls-15.8.4" title="15.8.4.&nbsp;Qualified this">&sect;15.8.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-H"></a>
To the left of <code class="literal">.</code><code class="literal">super</code> in a qualified superclass field
access expression (<a class="xref" href="jls-15.html#jls-15.11.2" title="15.11.2.&nbsp;Accessing Superclass Members using super">&sect;15.11.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-I"></a>
To the left of <code class="literal">.</code><span class="emphasis"><em>Identifier</em></span> or
<code class="literal">.</code><code class="literal">super</code><code class="literal">.</code><span class="emphasis"><em>Identifier</em></span> in a qualified method invocation
expression (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-J"></a>
To the left of <code class="literal">.</code><code class="literal">super</code><code class="literal">::</code> in a method reference
expression (<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-K"></a>
In a qualified expression name in a postfix expression
(<a class="xref" href="jls-15.html#jls-15.14.1" title="15.14.1.&nbsp;Expression Names">&sect;15.14.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-L"></a>
In a <code class="literal">throws</code> clause of a method or constructor
(<a class="xref" href="jls-8.html#jls-8.4.6" title="8.4.6.&nbsp;Method Throws">&sect;8.4.6</a>, <a class="xref" href="jls-8.html#jls-8.8.5" title="8.8.5.&nbsp;Constructor Throws">&sect;8.8.5</a>,
<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.1-210-M"></a>
In an exception parameter declaration
(<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>)
</p>
</li>
</ol>
</div>
<p class="norm"><a name="jls-6.1-220"></a>The first ten non-generic contexts
correspond to the first ten syntactic contexts for a <span class="emphasis"><em>TypeName</em></span> in
<a class="xref" href="jls-6.html#jls-6.5.1" title="6.5.1.&nbsp;Syntactic Classification of a Name According to Context">&sect;6.5.1</a>. The eleventh non-generic context is a
postfix expression, where a qualified <span class="emphasis"><em>ExpressionName</em></span> such
as <code class="literal">C.x</code> may include a
<span class="emphasis"><em>TypeName</em></span> <code class="literal">C</code> to denote static member access. The
common use of <span class="emphasis"><em>TypeName</em></span> is significant: it indicates that these
contexts involve a less-than-first-class use of a type. In contrast,
the twelfth and thirteenth non-generic contexts employ <span class="emphasis"><em>ClassType</em></span>,
indicating that <code class="literal">throws</code> and <code class="literal">catch</code> clauses use types in a
first-class way, in line with, say, field declarations. The
characterization of these two contexts as non-generic is due to the
fact that an exception type cannot be parameterized.
</p>
<p class="note">
Note that the <span class="emphasis"><em>ClassType</em></span> production allows annotations, so it is
possible to annotate the use of a type in a <code class="literal">throws</code> or <code class="literal">catch</code>
clause, whereas the <span class="emphasis"><em>TypeName</em></span> production disallows annotations, so it
is not possible to annotate the name of a type in, say, a
single-type-import declaration.
</p>
<p class="note"><span class="emphasis"><em>Naming Conventions</em></span></p>
<p class="note">The class libraries of the Java SE platform attempt to
use, whenever possible, names chosen according to the conventions
presented below. These conventions help to make code more readable and
avoid certain kinds of name conflicts.
</p>
<p class="note">We recommend these conventions for use in all
programs written in the Java programming language. However, these
conventions should not be followed slavishly if long-held conventional
usage dictates otherwise. So, for example, the <code class="literal">sin</code>
and <code class="literal">cos</code> methods of the
class <code class="literal">java.lang.Math</code> have mathematically
conventional names, even though these method names flout the
convention suggested here because they are short and are not
verbs.
</p>
<p class="note"><span class="emphasis"><em>Package Names</em></span></p>
<p class="note">Developers should take steps to avoid the
possibility of two published packages having the same name by
choosing <span class="emphasis"><em>unique package names</em></span> for packages that
are widely distributed. This allows packages to be easily and
automatically installed and catalogued. This section specifies a
suggested convention for generating such unique package
names. Implementations of the Java SE platform are encouraged to provide
automatic support for converting a set of packages from local and
casual package names to the unique name format described here.
</p>
<p class="note">If unique package names are not used, then package
name conflicts may arise far from the point of creation of either of
the conflicting packages. This may create a situation that is
difficult or impossible for the user or programmer to resolve. The
class <code class="literal">ClassLoader</code> can be used to isolate packages with the same name
from each other in those cases where the packages will have
constrained interactions, but not in a way that is transparent to a
na&iuml;ve program.
</p>
<p class="note">You form a unique package name by first having (or
belonging to an organization that has) an Internet domain name, such
as <code class="literal">oracle.com</code>. You then reverse this name,
component by component, to obtain, in this
example, <code class="literal">com.oracle</code>, and use this as a prefix for
your package names, using a convention developed within your
organization to further administer package names. Such a convention
might specify that certain package name components be
division, department, project, machine, or login names.
</p>
<div class="example"><a name="d5e8161"></a><p class="title"><b>Example&nbsp;6.1-1.&nbsp;Unique Package Names</b></p>
<div class="example-contents"><pre class="screen">
com.nighthacks.java.jag.scrabble
org.openjdk.tools.compiler
net.jcip.annotations
edu.cmu.cs.bovik.cheese
gov.whitehouse.socks.mousefinder
</pre></div>
</div><br class="example-break"><p class="note">The first component of a unique package name is
always written in all-lowercase ASCII letters and should be one of the
top level domain names, such as <code class="literal">com</code>,
<code class="literal">edu</code>, <code class="literal">gov</code>, <code class="literal">mil</code>,
<code class="literal">net</code>, or <code class="literal">org</code>, or one of the
English two-letter codes identifying countries as specified
in <em class="citetitle">ISO Standard 3166</em>.
</p>
<p class="note">The name of a package is not meant to imply where
the package is stored on the Internet. The suggested convention for
generating unique package names is merely a way to piggyback a package
naming convention on top of an existing, widely known unique name
registry instead of having to create a separate registry for package
names.
</p>
<div class="informalexample">
<p class="note">For example, a package
named <code class="literal">edu.cmu.cs.bovik.cheese</code> is not necessarily
obtainable from Internet address <code class="literal">cmu.edu</code>
or <code class="literal">cs.cmu.edu</code>
or <code class="literal">bovik.cs.cmu.edu</code>.
</p>
</div>
<p class="note">In some cases, the Internet domain name may not be a
valid package name. Here are some suggested conventions for dealing
with these situations:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">If the domain name contains a hyphen, or any
other special character not allowed in an identifier
(<a class="xref" href="jls-3.html#jls-3.8" title="3.8.&nbsp;Identifiers">&sect;3.8</a>), convert it into an
underscore.
</p>
</li>
<li class="listitem">
<p class="note">If any of the resulting package name components
are keywords (<a class="xref" href="jls-3.html#jls-3.9" title="3.9.&nbsp;Keywords">&sect;3.9</a>), append an underscore
to them.
</p>
</li>
<li class="listitem">
<p class="note">If any of the resulting package name components
start with a digit, or any other character that is not allowed
as an initial character of an identifier, have an underscore
prefixed to the component.
</p>
</li>
</ul>
</div>
<p class="note">Names of packages intended only for local use should
have a first identifier that begins with a lowercase letter, but that
first identifier specifically should not be the
identifier <code class="literal">java</code>; package names that start with the
identifier <code class="literal">java</code> are reserved for package of the
Java SE platform.
</p>
<p class="note"><span class="emphasis"><em>Class and Interface Type
Names</em></span></p>
<p class="note">Names of class types should be descriptive nouns or
noun phrases, not overly long, in mixed case with the first letter of
each word capitalized.
</p>
<div class="example"><a name="d5e8195"></a><p class="title"><b>Example&nbsp;6.1-2.&nbsp;Descriptive Class Names</b></p>
<div class="example-contents"><pre class="screen">
<code class="literal">ClassLoader</code>
SecurityManager
<code class="literal">Thread</code>
Dictionary
BufferedInputStream
</pre></div>
</div><br class="example-break"><p class="note">Likewise, names of interface types should be short
and descriptive, not overly long, in mixed case with the first letter
of each word capitalized. The name may be a descriptive noun or noun
phrase, which is appropriate when an interface is used as if it were
an abstract superclass, such as interfaces
<code class="literal">java.io.DataInput</code> and
<code class="literal">java.io.DataOutput</code>; or it may be an adjective
describing a behavior, as for the interfaces
<code class="literal">Runnable</code> and <code class="literal">Cloneable</code>.
</p>
<p class="note"><span class="emphasis"><em>Type Variable Names</em></span></p>
<p class="note">Type variable names should be pithy (single
character if possible) yet evocative, and should not include lower
case letters. This makes it easy to distinguish type parameters from
ordinary classes and interfaces.
</p>
<p class="note">Container types should use the
name <code class="literal">E</code> for their element type. Maps should
use <code class="literal">K</code> for the type of their keys
and <code class="literal">V</code> for the type of their values. The
name <code class="literal">X</code> should be used for arbitrary exception
types. We use <code class="literal">T</code> for type, whenever there is not
anything more specific about the type to distinguish it. (This is
often the case in generic methods.)
</p>
<p class="note">If there are multiple type parameters that denote
arbitrary types, one should use letters that
neighbor <code class="literal">T</code> in the alphabet, such
as <code class="literal">S</code>. Alternately, it is acceptable to use numeric
subscripts (e.g., <code class="literal">T1</code>, <code class="literal">T2</code>) to
distinguish among the different type variables. In such cases, all the
variables with the same prefix should be subscripted.
</p>
<p class="note">If a generic method appears inside a generic class,
it is a good idea to avoid using the same names for the type
parameters of the method and class, to avoid confusion. The same
applies to nested generic classes.
</p>
<div class="example"><a name="d5e8220"></a><p class="title"><b>Example&nbsp;6.1-3.&nbsp;Conventional Type Variable Names</b></p>
<div class="example-contents"><pre class="programlisting">
public class HashSet&lt;E&gt; extends AbstractSet&lt;E&gt; { ... }
public class HashMap&lt;K,V&gt; extends AbstractMap&lt;K,V&gt; { ... }
public class ThreadLocal&lt;T&gt; { ... }
public interface Functor&lt;T, X extends Throwable&gt; {
T eval() throws X;
}
</pre></div>
</div><br class="example-break"><p class="note">When type parameters do not fall conveniently into
one of the categories mentioned, names should be chosen to be as
meaningful as possible within the confines of a single letter. The
names mentioned above (<code class="literal">E</code>, <code class="literal">K</code>,
<code class="literal">V</code>, <code class="literal">X</code>, <code class="literal">T</code>)
should not be used for type parameters that do not fall into the
designated categories.
</p>
<p class="note"><span class="emphasis"><em>Method Names</em></span></p>
<p class="note">Method names should be verbs or verb phrases, in
mixed case, with the first letter lowercase and the first letter of
any subsequent words capitalized. Here are some additional specific
conventions for method names:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">Methods to get and set an attribute that might
be thought of as a variable <span class="emphasis"><em>V</em></span> should be
named <code class="literal">get<span class="emphasis"><em>V</em></span></code>
and <code class="literal">set<span class="emphasis"><em>V</em></span></code>. An example is
the methods <code class="literal">getPriority</code>
and <code class="literal">setPriority</code> of class <code class="literal">Thread</code>.
</p>
</li>
<li class="listitem">
<p class="note">A method that returns the length of something
should be named <code class="literal">length</code>, as in class
<code class="literal">String</code>.
</p>
</li>
<li class="listitem">
<p class="note">A method that tests a boolean
condition <span class="emphasis"><em>V</em></span> about an object should be
named <code class="literal">is<span class="emphasis"><em>V</em></span></code>. An example is
the method <code class="literal">isInterrupted</code> of class
<code class="literal">Thread</code>.
</p>
</li>
<li class="listitem">
<p class="note">A method that converts its object to a
particular format <span class="emphasis"><em>F</em></span> should be
named <code class="literal">to<span class="emphasis"><em>F</em></span></code>. Examples are
the method <code class="literal">toString</code> of class <code class="literal">Object</code> and the
methods <code class="literal">toLocaleString</code>
and <code class="literal">toGMTString</code> of
class <code class="literal">java.util.Date</code>.
</p>
</li>
</ul>
</div>
<p class="note">Whenever possible and appropriate, basing the names
of methods in a new class on names in an existing class that is
similar, especially a class from the Java SE platform API, will make it
easier to use.
</p>
<p class="note"><span class="emphasis"><em>Field Names</em></span></p>
<p class="note">Names of fields that are not <code class="literal">final</code> should be in
mixed case with a lowercase first letter and the first letters of
subsequent words capitalized. Note that well-designed classes have
very few <code class="literal">public</code> or <code class="literal">protected</code> fields, except for fields that are
constants (<code class="literal">static</code> <code class="literal">final</code> fields).
</p>
<p class="note">Fields should have names that are nouns, noun
phrases, or abbreviations for nouns.
</p>
<div class="informalexample">
<p class="note">Examples of this convention are the
fields <code class="literal">buf</code>, <code class="literal">pos</code>,
and <code class="literal">count</code> of the
class <code class="literal">java.io.ByteArrayInputStream</code> and the
field <code class="literal">bytesTransferred</code> of the
class <code class="literal">java.io.InterruptedIOException</code>.
</p>
</div>
<p class="note"><span class="emphasis"><em>Constant Names</em></span></p>
<p class="note">The names of constants in interface types should be,
and <code class="literal">final</code> variables of class types may conventionally be, a sequence
of one or more words, acronyms, or abbreviations, all uppercase, with
components separated by underscore "<code class="literal">_</code>"
characters. Constant names should be descriptive and not unnecessarily
abbreviated. Conventionally they may be any appropriate part of
speech.
</p>
<div class="informalexample">
<p class="note">Examples of names for constants
include <code class="literal">MIN_VALUE</code>, <code class="literal">MAX_VALUE</code>,
<code class="literal">MIN_RADIX</code>, and <code class="literal">MAX_RADIX</code> of the
class <code class="literal">Character</code>.
</p>
</div>
<p class="note">A group of constants that represent alternative
values of a set, or, less frequently, masking bits in an integer
value, are sometimes usefully specified with a common acronym as a
name prefix.
</p>
<div class="informalexample">
<p class="note">For example:</p><pre class="programlisting">
interface ProcessStates {
int PS_RUNNING = 0;
int PS_SUSPENDED = 1;
}
</pre></div>
<p class="note"><span class="emphasis"><em>Local Variable and Parameter
Names</em></span></p>
<p class="note">Local variable and parameter names should be short,
yet meaningful. They are often short sequences of lowercase letters
that are not words, such as:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">Acronyms, that is the first letter of a series
of words, as in <code class="literal">cp</code> for a variable holding a
reference to a <code class="literal">ColoredPoint</code></p>
</li>
<li class="listitem">
<p class="note">Abbreviations, as in <code class="literal">buf</code>
holding a pointer to a buffer of some kind
</p>
</li>
<li class="listitem">
<p class="note">Mnemonic terms, organized in some way to aid
memory and understanding, typically by using a set of local
variables with conventional names patterned after the names of
parameters to widely used classes. For example:
</p>
<div class="note">
<ul class="note" type="circle">
<li class="listitem">
<p class="note"><code class="literal">in</code>
and <code class="literal">out</code>, whenever some kind of input and
output are involved, patterned after the fields
of <code class="literal">System</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">off</code>
and <code class="literal">len</code>, whenever an offset and length
are involved, patterned after the parameters to
the <code class="literal">read</code> and <code class="literal">write</code>
methods of the interfaces <code class="literal">DataInput</code>
and <code class="literal">DataOutput</code>
of <code class="literal">java.io</code></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="note">One-character local variable or parameter names
should be avoided, except for temporary and looping variables, or
where a variable holds an undistinguished value of a
type. Conventional one-character names are:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note"><code class="literal">b</code> for a <code class="literal">byte</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">c</code> for a <code class="literal">char</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">d</code> for a <code class="literal">double</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">e</code> for an <code class="literal">Exception</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">f</code> for a <code class="literal">float</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">i</code>, <code class="literal">j</code>,
and <code class="literal">k</code> for <code class="literal">int</code>s
</p>
</li>
<li class="listitem">
<p class="note"><code class="literal">l</code> for a <code class="literal">long</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">o</code> for an <code class="literal">Object</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">s</code> for a <code class="literal">String</code></p>
</li>
<li class="listitem">
<p class="note"><code class="literal">v</code> for an arbitrary value of
some type
</p>
</li>
</ul>
</div>
<p class="note">Local variable or parameter names that consist of
only two or three lowercase letters should not conflict with the
initial country codes and domain names that are the first component of
unique package names.
</p>
</div>
<div class="section" title="6.2.&nbsp;Names and Identifiers">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-6.2"></a>6.2.&nbsp;Names and Identifiers
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-6.2-100"></a>A <span class="emphasis"><em>name</em></span> is
used to refer to an entity declared in a program.
</p>
<p class="norm"><a name="jls-6.2-110"></a>There are two forms of names:
simple names and qualified names.
</p>
<p class="norm"><a name="jls-6.2-120"></a>A <span class="emphasis"><em>simple
name</em></span> is a single identifier.
</p>
<p class="norm"><a name="jls-6.2-130"></a>A <span class="emphasis"><em>qualified
name</em></span> consists of a name, a "<code class="literal">.</code>" token, and an
identifier.
</p>
<p class="norm"><a name="jls-6.2-200"></a>In determining the meaning of a
name (<a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a>), the context in which the name
appears is taken into account. The rules of <a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a>
distinguish among contexts where a name must denote (refer to) a
package (<a class="xref" href="jls-6.html#jls-6.5.3" title="6.5.3.&nbsp;Meaning of Package Names">&sect;6.5.3</a>), a type
(<a class="xref" href="jls-6.html#jls-6.5.5" title="6.5.5.&nbsp;Meaning of Type Names">&sect;6.5.5</a>), a variable or value in an expression
(<a class="xref" href="jls-6.html#jls-6.5.6" title="6.5.6.&nbsp;Meaning of Expression Names">&sect;6.5.6</a>), or a method
(<a class="xref" href="jls-6.html#jls-6.5.7" title="6.5.7.&nbsp;Meaning of Method Names">&sect;6.5.7</a>).
</p>
<p class="note">Packages and reference types
have <span class="emphasis"><em>members</em></span> which may be accessed by qualified
names. As background for the discussion of qualified names and the
determination of the meaning of names, see the descriptions of
membership in <a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a>, <a class="xref" href="jls-4.html#jls-4.5.2" title="4.5.2.&nbsp;Members and Constructors of Parameterized Types">&sect;4.5.2</a>,
<a class="xref" href="jls-4.html#jls-4.8" title="4.8.&nbsp;Raw Types">&sect;4.8</a>, <a class="xref" href="jls-4.html#jls-4.9" title="4.9.&nbsp;Intersection Types">&sect;4.9</a>,
<a class="xref" href="jls-7.html#jls-7.1" title="7.1.&nbsp;Package Members">&sect;7.1</a>, <a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>,
<a class="xref" href="jls-9.html#jls-9.2" title="9.2.&nbsp;Interface Members">&sect;9.2</a>, and <a class="xref" href="jls-10.html#jls-10.7" title="10.7.&nbsp;Array Members">&sect;10.7</a>.
</p>
<p class="norm"><a name="jls-6.2-300"></a>Not all identifiers in a
program are a part of a name. Identifiers are also used in the
following situations:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-A"></a>
In declarations (<a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a>), where an identifier
may occur to specify the name by which the declared entity will
be known.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-B"></a>
As labels in labeled statements (<a class="xref" href="jls-14.html#jls-14.7" title="14.7.&nbsp;Labeled Statements">&sect;14.7</a>) and
in <code class="literal">break</code> and <code class="literal">continue</code> statements
(<a class="xref" href="jls-14.html#jls-14.15" title="14.15.&nbsp;The break Statement">&sect;14.15</a>, <a class="xref" href="jls-14.html#jls-14.16" title="14.16.&nbsp;The continue Statement">&sect;14.16</a>) that
refer to statement labels.
</p>
<p class="norm"><a name="jls-6.2-300-B.1"></a>
The identifiers used in labeled statements and their associated
<code class="literal">break</code> and <code class="literal">continue</code> statements are completely separate from
those used in declarations.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-C"></a>
In field access expressions (<a class="xref" href="jls-15.html#jls-15.11" title="15.11.&nbsp;Field Access Expressions">&sect;15.11</a>), where
an identifier occurs after a "<code class="literal">.</code>" token to indicate a member
of the object denoted by the expression before the "<code class="literal">.</code>"
token, or the object denoted by the <code class="literal">super</code> or
<span class="emphasis"><em>TypeName</em></span><code class="literal">.</code><code class="literal">super</code> before the "<code class="literal">.</code>" token.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-D"></a>
In some method invocation expressions
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>), wherever an identifier occurs
after a "<code class="literal">.</code>" token and before a "<code class="literal">(</code>" token to indicate
a method to be invoked for the object denoted by the expression
before the "<code class="literal">.</code>" token, or the type denoted by the <span class="emphasis"><em>TypeName</em></span>
before the "<code class="literal">.</code>" token, or the object denoted by the <code class="literal">super</code>
or <span class="emphasis"><em>TypeName</em></span><code class="literal">.</code><code class="literal">super</code> before the "<code class="literal">.</code>" token.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-E"></a>
In some method reference expressions
(<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>), wherever an identifier occurs
after a "<code class="literal">::</code>" token to indicate a method of the object
denoted by the expression before the "<code class="literal">::</code>" token, or
the type denoted by the <span class="emphasis"><em>TypeName</em></span> before the "<code class="literal">::</code>"
token, or the object denoted by the <code class="literal">super</code> or
<span class="emphasis"><em>TypeName</em></span><code class="literal">.</code><code class="literal">super</code> before the "<code class="literal">::</code>" token.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-F"></a>
In qualified class instance creation expressions
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>), where an identifier occurs to the
right of the <code class="literal">new</code> token to indicate a type that is a member of
the compile-time type of the expression preceding the <code class="literal">new</code>
token.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-6.2-300-G"></a>
In element-value pairs of annotations
(<a class="xref" href="jls-9.html#jls-9.7.1" title="9.7.1.&nbsp;Normal Annotations">&sect;9.7.1</a>), to denote an element of the
corresponding annotation type.
</p>
</li>
</ul>
</div>
<div class="informalexample">
<p class="note">In this program:</p><pre class="programlisting">
class Test {
public static void main(String[] args) {
Class c = System.out.getClass();
System.out.println(c.toString().length() +
args[0].length() + args.length);
}
}
</pre><p class="note">the
identifiers <code class="literal">Test</code>, <code class="literal">main</code>, and the
first occurrences of <code class="literal">args</code> and <code class="literal">c</code>
are not names. Rather, they are identifiers used in declarations to
specify the names of the declared entities. The names <code class="literal">String</code>,
<code class="literal">Class</code>, <code class="literal">System.out.getClass</code>,
<code class="literal">System.out.println</code>, <code class="literal">c.toString</code>,
<code class="literal">args</code>, and <code class="literal">args.length</code> appear in
the example.
</p>
<p class="note">
The occurrence of <code class="literal">length</code> in <code class="literal">args.length</code> is a name
because <code class="literal">args.length</code> is a qualified name
(<a class="xref" href="jls-6.html#jls-6.5.6.2" title="6.5.6.2.&nbsp;Qualified Expression Names">&sect;6.5.6.2</a>) and not a field access expression
(<a class="xref" href="jls-15.html#jls-15.11" title="15.11.&nbsp;Field Access Expressions">&sect;15.11</a>). A field access expression, as well as a
method invocation expression, a method reference expression, and a
qualified class instance creation expression, uses an identifier
rather than a name to denote the member of interest. Thus, the
occurrence of <code class="literal">length</code> in <code class="literal">args[0].length()</code>
is <span class="emphasis"><em>not</em></span> a name, but rather an identifier appearing
in a method invocation expression.
</p>
</div>
<p class="note">
One might wonder why these kinds of expression use an identifier
rather than a simple name, which is after all just an identifier. The
reason is that a simple expression name is defined in terms of the
lexical environment; that is, a simple expression name must be in the
scope of a variable declaration (<a class="xref" href="jls-6.html#jls-6.5.6.1" title="6.5.6.1.&nbsp;Simple Expression Names">&sect;6.5.6.1</a>). On
the other hand, field access, qualified method invocation, method
references, and qualified class instance creation all refer to members
whose names are not in the lexical environment. By definition, such
names are bound only in the context provided by the <span class="emphasis"><em>Primary</em></span> of the
field access expression, method invocation expression, method
reference expression, or class instance creation expression; or by the
<code class="literal">super</code> of the field access expression, method invocation expression,
or method reference expression; and so on. Thus, we denote such
members with identifiers rather than simple names.
</p>
<p class="note">To complicate
things further, a field access expression is not the only way to
denote a field of an object. For parsing reasons, a qualified name is
used to denote a field of an in-scope variable. (The variable itself
is denoted with a simple name, alluded to above.) It is necessary for
access control (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) to apply to both
denotations of a field.
</p>
</div>
<div class="section" title="6.3.&nbsp;Scope of a Declaration">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-6.3"></a>6.3.&nbsp;Scope of a Declaration
</h2>
</div>
</div>
</div>
<p class="norm"><a name="jls-6.3-100"></a>The <span class="emphasis"><em>scope</em></span>
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,
provided it is visible (<a class="xref" href="jls-6.html#jls-6.4.1" title="6.4.1.&nbsp;Shadowing">&sect;6.4.1</a>).
</p>
<p class="norm"><a name="jls-6.3-110"></a>A declaration is said to
be <span class="emphasis"><em>in scope</em></span> at a particular point in a program if
and only if the declaration's scope includes that point.
</p>
<p class="norm-static"><a name="jls-6.3-200"></a>The scope of
the declaration of an observable (<a class="xref" href="jls-7.html#jls-7.4.3" title="7.4.3.&nbsp;Observability of a Package">&sect;7.4.3</a>) top
level package is all observable compilation units
(<a class="xref" href="jls-7.html#jls-7.3" title="7.3.&nbsp;Compilation Units">&sect;7.3</a>).
</p>
<p class="norm-static"><a name="jls-6.3-210"></a>The
declaration of a package that is not observable is never in
scope.
</p>
<p class="norm-static"><a name="jls-6.3-220"></a>The
declaration of a subpackage is never in scope.
</p>
<p class="norm-static"><a name="jls-6.3-230"></a>The
package <code class="literal">java</code> is always in scope.
</p>
<p class="norm-static"><a name="jls-6.3-240"></a>The scope of
a type imported by a single-type-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.1" title="7.5.1.&nbsp;Single-Type-Import Declarations">&sect;7.5.1</a>) or a type-import-on-demand declaration
(<a class="xref" href="jls-7.html#jls-7.5.2" title="7.5.2.&nbsp;Type-Import-on-Demand Declarations">&sect;7.5.2</a>) is all the class and interface type
declarations (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) in the compilation unit in
which the <code class="literal">import</code> declaration appears, as well as any
annotations on the package declaration (if any) of the compilation
unit .
</p>
<p class="norm-static"><a name="jls-6.3-250"></a>The scope of
a member imported by a single-static-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.3" title="7.5.3.&nbsp;Single-Static-Import Declarations">&sect;7.5.3</a>) or a static-import-on-demand declaration
(<a class="xref" href="jls-7.html#jls-7.5.4" title="7.5.4.&nbsp;Static-Import-on-Demand Declarations">&sect;7.5.4</a>) is all the class and interface type
declarations (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) in the compilation unit in
which the <code class="literal">import</code> declaration appears, as well as any
annotations on the package declaration (if any) of the compilation
unit .
</p>
<p class="norm-static"><a name="jls-6.3-300"></a>The scope of
a top level type (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) is all type declarations
in the package in which the top level type is declared.
</p>
<p class="norm-static"><a name="jls-6.3-310"></a>The scope of
a declaration of a member <code class="varname">m</code> declared in or
inherited by a class type <span class="type">C</span> (<a class="xref" href="jls-8.html#jls-8.1.6" title="8.1.6.&nbsp;Class Body and Member Declarations">&sect;8.1.6</a>) is the
entire body of <span class="type">C</span>, including any nested type declarations.
</p>
<p class="norm-static"><a name="jls-6.3-320"></a>The scope of
a declaration of a member <code class="varname">m</code> declared in or
inherited by an interface type <span class="type">I</span> (<a class="xref" href="jls-9.html#jls-9.1.4" title="9.1.4.&nbsp;Interface Body and Member Declarations">&sect;9.1.4</a>) is
the entire body of <span class="type">I</span>, including any nested type declarations.
</p>
<p class="norm-static"><a name="jls-6.3-330"></a>The scope of
an enum constant <span class="type">C</span> declared in an enum type <span class="type">T</span> is the body of <span class="type">T</span>,
and any <code class="literal">case</code> label of a <code class="literal">switch</code> statement whose expression is of
enum type <span class="type">T</span> (<a class="xref" href="jls-14.html#jls-14.11" title="14.11.&nbsp;The switch Statement">&sect;14.11</a>).
</p>
<p class="norm-static"><a name="jls-6.3-340"></a>The scope of a
formal parameter of a method (<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>),
constructor (<a class="xref" href="jls-8.html#jls-8.8.1" title="8.8.1.&nbsp;Formal Parameters">&sect;8.8.1</a>), or lambda expression
(<a class="xref" href="jls-15.html#jls-15.27" title="15.27.&nbsp;Lambda Expressions">&sect;15.27</a>) is the entire body of the method,
constructor, or lambda expression.
</p>
<p class="norm-static"><a name="jls-6.3-400"></a>The
scope of a class's type parameter (<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>)
is the type parameter section of
the class declaration, the type parameter section of any superclass or
superinterface of the class declaration, and the class
body.
</p>
<p class="norm-static"><a name="jls-6.3-410"></a>The
scope of an interface's type parameter (<a class="xref" href="jls-9.html#jls-9.1.2" title="9.1.2.&nbsp;Generic Interfaces and Type Parameters">&sect;9.1.2</a>)
is the type parameter section of
the interface declaration, the type parameter section of any
superinterface of the interface declaration, and the interface
body.
</p>
<p class="norm-static"><a name="jls-6.3-420"></a>The
scope of a method's type parameter (<a class="xref" href="jls-8.html#jls-8.4.4" title="8.4.4.&nbsp;Generic Methods">&sect;8.4.4</a>) is
the entire declaration of the method, including the type parameter
section, but excluding the method
modifiers.
</p>
<p class="norm-static"><a name="jls-6.3-430"></a>The
scope of a constructor's type parameter (<a class="xref" href="jls-8.html#jls-8.8.4" title="8.8.4.&nbsp;Generic Constructors">&sect;8.8.4</a>)
is the entire declaration of the constructor, including the type
parameter section, but excluding the constructor
modifiers.
</p>
<p class="norm-static"><a name="jls-6.3-500"></a>The scope of
a local class declaration immediately enclosed by a block
(<a class="xref" href="jls-14.html#jls-14.2" title="14.2.&nbsp;Blocks">&sect;14.2</a>) is the rest of the immediately enclosing
block, including its own class declaration.
</p>
<p class="norm-static"><a name="jls-6.3-510"></a>The scope of
a local class declaration immediately enclosed by a switch block
statement group (<a class="xref" href="jls-14.html#jls-14.11" title="14.11.&nbsp;The switch Statement">&sect;14.11</a>) is the rest of the
immediately enclosing switch block statement group, including its own
class declaration.
</p>
<p class="norm-static"><a name="jls-6.3-520"></a>The scope of
a local variable declaration in a block (<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>)
is the rest of the block in which the declaration appears, starting
with its own initializer and including any further declarators to the
right in the local variable declaration statement.
</p>
<p class="norm-static"><a name="jls-6.3-530"></a>The scope of
a local variable declared in the <span class="emphasis"><em>ForInit</em></span> part of
a basic <code class="literal">for</code> statement (<a class="xref" href="jls-14.html#jls-14.14.1" title="14.14.1.&nbsp;The basic for Statement">&sect;14.14.1</a>) includes all
of the following:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.3-530-A"></a>Its
own initializer
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.3-530-B"></a>Any
further declarators to the right in
the <span class="emphasis"><em>ForInit</em></span> part of the <code class="literal">for</code>
statement
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.3-530-C"></a>The <span class="emphasis"><em>Expression</em></span>
and <span class="emphasis"><em>ForUpdate</em></span> parts of the <code class="literal">for</code>
statement
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.3-530-D"></a>The
contained <span class="emphasis"><em>Statement</em></span></p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.3-540"></a>The scope of
a local variable declared in the <span class="emphasis"><em>FormalParameter</em></span>
part of an enhanced <code class="literal">for</code> statement (<a class="xref" href="jls-14.html#jls-14.14.2" title="14.14.2.&nbsp;The enhanced for statement">&sect;14.14.2</a>) is
the contained <span class="emphasis"><em>Statement</em></span>.
</p>
<p class="norm-static"><a name="jls-6.3-550"></a>The scope of
a parameter of an exception handler that is declared in a <code class="literal">catch</code>
clause of a <code class="literal">try</code> statement (<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>) is the
entire block associated with the <code class="literal">catch</code>.
</p>
<p class="norm-static"><a name="jls-6.3-560"></a>The scope of a
variable declared in the <span class="emphasis"><em>ResourceSpecification</em></span> of
a <code class="literal">try</code>-with-resources statement (<a class="xref" href="jls-14.html#jls-14.20.3" title="14.20.3.&nbsp;try-with-resources">&sect;14.20.3</a>) is
from the declaration rightward over the remainder of
the <span class="emphasis"><em>ResourceSpecification</em></span> and the entire <code class="literal">try</code>
block associated with the <code class="literal">try</code>-with-resources statement.
</p>
<p class="note">The translation of a <code class="literal">try</code>-with-resources statement
implies the rule above.
</p>
<div class="example"><a name="d5e8585"></a><p class="title"><b>Example&nbsp;6.3-1.&nbsp;Scope of Type Declarations</b></p>
<div class="example-contents">
<p class="note">These rules imply that declarations of class and
interface types need not appear before uses of the types. In the
following program, the use of <code class="literal">PointList</code> in
class <code class="literal">Point</code> is valid, because the scope of the
class declaration <code class="literal">PointList</code> includes both
class <code class="literal">Point</code> and class <code class="literal">PointList</code>,
as well as any other type declarations in other compilation units of
package <code class="literal">points</code>.
</p><pre class="programlisting">
package points;
class Point {
int x, y;
PointList list;
Point next;
}
class PointList {
Point first;
}
</pre></div>
</div><br class="example-break"><div class="example"><a name="d5e8595"></a><p class="title"><b>Example&nbsp;6.3-2.&nbsp;Scope of Local Variable Declarations</b></p>
<div class="example-contents">
<p class="note">The following program causes a compile-time error
because the initialization of local variable <code class="literal">x</code> is
within the scope of the declaration of local
variable <code class="literal">x</code>, but the local
variable <code class="literal">x</code> does not yet have a value and cannot be
used. The field <code class="literal">x</code> has a value of <code class="literal">0</code> (assigned
when <code class="literal">Test1</code> was initialized) but is a red herring
since it is shadowed (<a class="xref" href="jls-6.html#jls-6.4.1" title="6.4.1.&nbsp;Shadowing">&sect;6.4.1</a>) by the local
variable <code class="literal">x</code>.
</p><pre class="programlisting">
class Test1 {
static int x;
public static void main(String[] args) {
int x = x;
}
}
</pre><p class="note">The following program does compile:</p><pre class="programlisting">
class Test2 {
static int x;
public static void main(String[] args) {
int x = (x=2)*2;
System.out.println(x);
}
}
</pre><p class="note">because the local variable <code class="literal">x</code> is
definitely assigned (<a class="xref" href="jls-16.html" title="Chapter&nbsp;16.&nbsp;Definite Assignment">&sect;16 (<i>Definite Assignment</i>)</a>) before it is used. It
prints:
</p><pre class="screen">
4
</pre><p class="note">In the following program, the initializer
for <code class="literal">three</code> can correctly refer to the
variable <code class="literal">two</code> declared in an earlier declarator, and
the method invocation in the next line can correctly refer to the
variable <code class="literal">three</code> declared earlier in the
block.
</p><pre class="programlisting">
class Test3 {
public static void main(String[] args) {
System.out.print("2+1=");
int two = 2, three = two + 1;
System.out.println(three);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
2+1=3
</pre></div>
</div><br class="example-break"></div>
<div class="section" title="6.4.&nbsp;Shadowing and Obscuring">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-6.4"></a>6.4.&nbsp;Shadowing and Obscuring
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.4-100"></a>A local
variable (<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>), formal parameter
(<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>, <a class="xref" href="jls-15.html#jls-15.27.1" title="15.27.1.&nbsp;Lambda Parameters">&sect;15.27.1</a>),
exception parameter (<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>), and local class
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>) can only be referred to using a simple
name, not a qualified name (<a class="xref" href="jls-6.html#jls-6.2" title="6.2.&nbsp;Names and Identifiers">&sect;6.2</a>).
</p>
<p class="norm"><a name="jls-6.4-110"></a>Some declarations are not
permitted within the scope of a local variable, formal parameter,
exception parameter, or local class declaration because it would be
impossible to distinguish between the declared entities using only
simple names.
</p>
<p class="note">For example, if the name of a formal parameter of a
method could be redeclared as the name of a local variable in the
method body, then the local variable would shadow the formal parameter
and the formal parameter would no longer be visible - an undesirable
outcome.
</p>
<p class="norm-error"><a name="jls-6.4-200"></a>It is a
compile-time error if the name of a formal parameter
is used to declare a new variable within the body of the
method, constructor, or lambda expression, unless the new variable is
declared within a class declaration contained by the method,
constructor, or lambda expression.
</p>
<p class="norm-error"><a name="jls-6.4-210"></a>It is a
compile-time error if the name of a local variable <code class="varname">v</code>
is used to declare a new variable within the
scope of <code class="varname">v</code>, unless the new variable is declared within a class whose
declaration is within the scope of <code class="varname">v</code>.
</p>
<p class="norm-error"><a name="jls-6.4-220"></a>It is a
compile-time error if the name of an exception parameter
is used
to declare a new variable within the <span class="emphasis"><em>Block</em></span> of the <code class="literal">catch</code> clause,
unless the new variable is declared within a class declaration
contained by the <span class="emphasis"><em>Block</em></span> of the <code class="literal">catch</code> clause.
</p>
<p class="norm-error"><a name="jls-6.4-230"></a>It is a
compile-time error if the name of a local class <span class="type">C</span> is used to declare a new
local class within the scope of <span class="type">C</span>, unless the new local class is
declared within another class whose declaration is within the scope of
<span class="type">C</span>.
</p>
<p class="note">These rules allow redeclaration of a variable or
local class in nested class declarations (local classes
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>) and anonymous classes
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>)) that occur in the scope of the variable
or local class. Thus, the declaration of a formal parameter, local
variable, or local class may be shadowed in a class declaration nested
within a method, constructor, or lambda expression; and the
declaration of an exception parameter may be shadowed inside a class
declaration nested within the <span class="emphasis"><em>Block</em></span> of the <code class="literal">catch</code> clause.
</p>
<p class="note">There are two design alternatives for handling name
clashes created by lambda parameters and other variables declared in
lambda expressions. One is to mimic class declarations: like local
classes, lambda expressions introduce a new "level" for names, and all
variable names outside the expression can be redeclared. Another is a
"local" strategy: like <code class="literal">catch</code> clauses, <code class="literal">for</code> loops, and blocks,
lambda expressions operate at the same "level" as the enclosing
context, and local variables outside the expression cannot be
shadowed. The above rules use the local strategy; there is no special
dispensation that allows a variable declared in a lambda expression to
shadow a variable declared in an enclosing method.
</p>
<p class="note">Note that the rule for local classes does not make
an exception for a class of the same name declared within the local
class itself. However, this case is prohibited by a separate rule: a
class cannot have the same name as a class that encloses it
(<a class="xref" href="jls-8.html#jls-8.1" title="8.1.&nbsp;Class Declarations">&sect;8.1</a>).
</p>
<div class="example"><a name="d5e8655"></a><p class="title"><b>Example&nbsp;6.4-1.&nbsp;Attempted Shadowing Of A Local Variable</b></p>
<div class="example-contents">
<p class="note">Because a
declaration of an identifier as a local variable of a method,
constructor, or initializer block must not appear within the scope of
a parameter or local variable of the same name, a compile-time error
occurs for the following program:
</p><pre class="programlisting">
class Test1 {
public static void main(String[] args) {
int i;
for (int i = 0; i &lt; 10; i++)
System.out.println(i);
}
}
</pre><p class="note">This restriction helps to detect some otherwise very
obscure bugs. A similar restriction on shadowing of members by local
variables was judged impractical, because the addition of a member in
a superclass could cause subclasses to have to rename local
variables. Related considerations make restrictions on shadowing of
local variables by members of nested classes, or on shadowing of local
variables by local variables declared within nested classes
unattractive as well.
</p>
<p class="note">Hence, the following program compiles without
error:
</p><pre class="programlisting">
class Test2 {
public static void main(String[] args) {
int i;
class Local {
{
for (int i = 0; i &lt; 10; i++)
System.out.println(i);
}
}
new Local();
}
}
</pre><p class="note">On the other hand, local variables with the same
name may be declared in two separate blocks or <code class="literal">for</code> statements,
neither of which contains the other:
</p><pre class="programlisting">
class Test3 {
public static void main(String[] args) {
for (int i = 0; i &lt; 10; i++)
System.out.print(i + " ");
for (int i = 10; i &gt; 0; i--)
System.out.print(i + " ");
System.out.println();
}
}
</pre><p class="note">This program compiles without error and, when
executed, produces the output:
</p><pre class="screen">
0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
</pre></div>
</div><br class="example-break"><div class="section" title="6.4.1.&nbsp;Shadowing">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-6.4.1"></a>6.4.1.&nbsp;Shadowing
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.4.1-100"></a>Some
declarations may be <span class="emphasis"><em>shadowed</em></span> 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>
<p class="norm"><a name="jls-6.4.1-110"></a>Shadowing is distinct from
hiding (<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>, <a class="xref" href="jls-8.html#jls-8.4.8.2" title="8.4.8.2.&nbsp;Hiding (by Class Methods)">&sect;8.4.8.2</a>,
<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>, <a class="xref" href="jls-9.html#jls-9.3" title="9.3.&nbsp;Field (Constant) Declarations">&sect;9.3</a>,
<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>), which applies only to members which would
otherwise be inherited but are not because of a declaration in a
subclass. Shadowing is also distinct from obscuring
(<a class="xref" href="jls-6.html#jls-6.4.2" title="6.4.2.&nbsp;Obscuring">&sect;6.4.2</a>).
</p>
<p class="norm"><a name="jls-6.4.1-200"></a>A declaration <span class="type">d</span>
is said to be <span class="emphasis"><em>visible at point <code class="varname">p</code> in a
program</em></span> if the scope of <span class="type">d</span>
includes <code class="varname">p</code>, and <span class="type">d</span> is not shadowed by
any other declaration at <code class="varname">p</code>.
</p>
<p class="norm"><a name="jls-6.4.1-210"></a>When the program point we are
discussing is clear from context, we will often simply say that a
declaration is <span class="emphasis"><em>visible</em></span>.
</p>
<p class="norm-static"><a name="jls-6.4.1-300"></a>A
declaration <span class="type">d</span> of a type named <span class="type">n</span> shadows the
declarations of any other types named <span class="type">n</span> that are in scope
at the point where <span class="type">d</span> occurs throughout the scope
of <span class="type">d</span>.
</p>
<p class="norm-static"><a name="jls-6.4.1-310"></a>A declaration <span class="type">d</span> of a
field or formal parameter named <span class="type">n</span> shadows, throughout the
scope of <span class="type">d</span>, the declarations of any other variables
named <span class="type">n</span> that are in scope at the point
where <span class="type">d</span> occurs.
</p>
<p class="norm-static"><a name="jls-6.4.1-320"></a>A declaration <span class="type">d</span> of a
local variable or exception parameter named <span class="type">n</span> shadows,
throughout the scope of <span class="type">d</span>, (a) the declarations of any
other fields named <span class="type">n</span> that are in scope at the point
where <span class="type">d</span> occurs, and (b) the declarations of any other
variables named <span class="type">n</span> that are in scope at the point
where <span class="type">d</span> occurs but are <span class="emphasis"><em>not</em></span> declared
in the innermost class in which <span class="type">d</span> is declared.
</p>
<p class="norm-static"><a name="jls-6.4.1-330"></a>A
declaration <span class="type">d</span> of a method named <span class="type">n</span> shadows
the declarations of any other methods named <span class="type">n</span> that are in
an enclosing scope at the point where <span class="type">d</span> occurs throughout
the scope of <span class="type">d</span>.
</p>
<p class="norm-static"><a name="jls-6.4.1-340"></a>A package
declaration never shadows any other declaration.
</p>
<p class="norm-static"><a name="jls-6.4.1-400"></a>A
type-import-on-demand declaration never causes any other declaration
to be shadowed.
</p>
<p class="norm-static"><a name="jls-6.4.1-410"></a>A
static-import-on-demand declaration never causes any other declaration
to be shadowed.
</p>
<p class="norm-static"><a name="jls-6.4.1-420"></a>A
single-type-import declaration <span class="type">d</span> in a compilation
unit <span class="type">c</span> of package <span class="type">p</span> that imports a type
named <span class="type">n</span> shadows, throughout <span class="type">c</span>, the
declarations of:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-420-A"></a>any
top level type named <span class="type">n</span> declared in another
compilation unit of <span class="type">p</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-420-B"></a>any
type named <span class="type">n</span> imported by a type-import-on-demand
declaration in <span class="type">c</span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-420-C"></a>any
type named <span class="type">n</span> imported by a static-import-on-demand
declaration in <span class="type">c</span></p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.4.1-430"></a>A
single-static-import declaration <code class="varname">d</code> in a compilation
unit <code class="varname">c</code> of package <code class="varname">p</code> that imports
a field named <code class="varname">n</code> shadows the declaration of any
static field named <code class="varname">n</code> imported by a
static-import-on-demand declaration in <code class="varname">c</code>,
throughout <code class="varname">c</code>.
</p>
<p class="norm-static"><a name="jls-6.4.1-440"></a>A
single-static-import declaration <code class="varname">d</code> in a compilation
unit <code class="varname">c</code> of package <code class="varname">p</code> that imports
a method named <code class="varname">n</code> with signature <span class="type">s</span>
shadows the declaration of any static method named <span class="type">n</span> with
signature <span class="type">s</span> imported by a static-import-on-demand
declaration in <code class="varname">c</code>,
throughout <code class="varname">c</code>.
</p>
<p class="norm-static"><a name="jls-6.4.1-450"></a>A
single-static-import declaration <code class="varname">d</code> in a compilation
unit <code class="varname">c</code> of package <code class="varname">p</code> that imports
a type named <span class="type">n</span> shadows, throughout <code class="varname">c</code>,
the declarations of:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-450-A"></a>any
static type named <span class="type">n</span> imported by a
static-import-on-demand declaration
in <code class="varname">c</code>;
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-450-B"></a>any
top level type (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) named <span class="type">n</span>
declared in another compilation unit (<a class="xref" href="jls-7.html#jls-7.3" title="7.3.&nbsp;Compilation Units">&sect;7.3</a>)
of <code class="varname">p</code>;
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.4.1-450-C"></a>any
type named <span class="type">n</span> imported by a type-import-on-demand
declaration (<a class="xref" href="jls-7.html#jls-7.5.2" title="7.5.2.&nbsp;Type-Import-on-Demand Declarations">&sect;7.5.2</a>)
in <code class="varname">c</code>.
</p>
</li>
</ul>
</div>
<div class="example"><a name="d5e8778"></a><p class="title"><b>Example&nbsp;6.4.1-1.&nbsp;Shadowing of a Field Declaration by a Local Variable Declaration</b></p>
<div class="example-contents"><pre class="programlisting">
class Test {
static int x = 1;
public static void main(String[] args) {
int x = 0;
System.out.print("x=" + x);
System.out.println(", Test.x=" + Test.x);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
x=0, Test.x=1
</pre><p class="note">This program declares:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">a class <code class="literal">Test</code></p>
</li>
<li class="listitem">
<p class="note">a class (<code class="literal">static</code>) variable <code class="literal">x</code>
that is a member of the class <code class="literal">Test</code></p>
</li>
<li class="listitem">
<p class="note">a class method <code class="literal">main</code> that is a
member of the class <code class="literal">Test</code></p>
</li>
<li class="listitem">
<p class="note">a parameter <code class="literal">args</code> of
the <code class="literal">main</code> method
</p>
</li>
<li class="listitem">
<p class="note">a local variable <code class="literal">x</code> of
the <code class="literal">main</code> method
</p>
</li>
</ul>
</div>
<p class="note">Since the scope of a class variable includes the
entire body of the class (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>), the class
variable <code class="literal">x</code> would normally be available throughout
the entire body of the method <code class="literal">main</code>. In this
example, however, the class variable <code class="literal">x</code> is shadowed
within the body of the method <code class="literal">main</code> by the
declaration of the local variable <code class="literal">x</code>.
</p>
<p class="note">A local variable has as its scope the rest of the
block in which it is declared (<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>); in this
case this is the rest of the body of the <code class="literal">main</code>
method, namely its initializer "<code class="literal">0</code>" and the invocations
of <code class="literal">System.out.print</code>
and <code class="literal">System.out.println</code>.
</p>
<p class="note">This means that:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">The expression <code class="literal">x</code> in the
invocation of <code class="literal">print</code> refers to (denotes) the
value of the local variable <code class="literal">x</code>.
</p>
</li>
<li class="listitem">
<p class="note">The invocation of <code class="literal">println</code>
uses a qualified name
(<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) <code class="literal">Test.x</code>, which uses
the class type name <code class="literal">Test</code> to access the class
variable <code class="literal">x</code>, because the declaration
of <code class="literal">Test.x</code> is shadowed at this point and cannot
be referred to by its simple name.
</p>
</li>
</ul>
</div>
<p class="note">The keyword <code class="literal">this</code> can also be used to access a
shadowed field <code class="literal">x</code>, using the
form <code class="literal">this.x</code>. Indeed, this idiom typically appears
in constructors (<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</a>):
</p><pre class="programlisting">
class Pair {
Object first, second;
public Pair(Object first, Object second) {
this.first = first;
this.second = second;
}
}
</pre><p class="note">Here, the constructor takes parameters having the
same names as the fields to be initialized. This is simpler than
having to invent different names for the parameters and is not too
confusing in this stylized context. In general, however, it is
considered poor style to have local variables with the same names as
fields.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e8840"></a><p class="title"><b>Example&nbsp;6.4.1-2.&nbsp;Shadowing of a Type Declaration by Another Type Declaration</b></p>
<div class="example-contents"><pre class="programlisting">
import java.util.*;
class Vector {
int val[] = { 1 , 2 };
}
class Test {
public static void main(String[] args) {
Vector v = new Vector();
System.out.println(v.val[0]);
}
}
</pre><p class="note">The program compiles and prints:</p><pre class="screen">
1
</pre><p class="note">using the class <code class="literal">Vector</code> declared
here in preference to the generic
class <code class="literal">java.util.Vector</code>
(<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>) that might be imported on demand.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="6.4.2.&nbsp;Obscuring">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-6.4.2"></a>6.4.2.&nbsp;Obscuring
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.4.2-100"></a>A simple
name may occur in contexts where it may potentially be interpreted as
the name of a variable, a type, or a package. In these situations, the
rules of <a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a> specify that a variable will be
chosen in preference to a type, and that a type will be chosen in
preference to a package. Thus, it is may sometimes be impossible to
refer to a visible type or package declaration via its simple name. We
say that such a declaration is <span class="emphasis"><em>obscured</em></span>.
</p>
<p class="norm"><a name="jls-6.4.2-110"></a>Obscuring is distinct from
shadowing (<a class="xref" href="jls-6.html#jls-6.4.1" title="6.4.1.&nbsp;Shadowing">&sect;6.4.1</a>) and hiding
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>, <a class="xref" href="jls-8.html#jls-8.4.8.2" title="8.4.8.2.&nbsp;Hiding (by Class Methods)">&sect;8.4.8.2</a>,
<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>, <a class="xref" href="jls-9.html#jls-9.3" title="9.3.&nbsp;Field (Constant) Declarations">&sect;9.3</a>,
<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>).
</p>
<p class="note">The naming conventions of <a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a>
help reduce obscuring, but if it does occur, here are some notes about
what you can do to avoid it.
</p>
<p class="note">When package names occur in expressions:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">If a package name is obscured by a field
declaration, then <code class="literal">import</code> declarations
(<a class="xref" href="jls-7.html#jls-7.5" title="7.5.&nbsp;Import Declarations">&sect;7.5</a>) can usually be used to make
available the type names declared in that package.
</p>
</li>
<li class="listitem">
<p class="note">If a package name is obscured by a declaration
of a parameter or local variable, then the name of the parameter
or local variable can be changed without affecting other
code.
</p>
</li>
</ul>
</div>
<p class="note">The first component of a package name is normally
not easily mistaken for a type name, as a type name normally begins
with a single uppercase letter. (The Java programming language does not actually rely
on case distinctions to determine whether a name is a package name or
a type name.)
</p>
<p class="note">Obscuring involving class and interface type names
is rare. Names of fields, parameters, and local variables normally do
not obscure type names because they conventionally begin with a
lowercase letter whereas type names conventionally begin with an
uppercase letter.
</p>
<p class="note">Method names cannot obscure or be obscured by other
names (<a class="xref" href="jls-6.html#jls-6.5.7" title="6.5.7.&nbsp;Meaning of Method Names">&sect;6.5.7</a>).
</p>
<p class="note">Obscuring involving field names is rare;
however:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">If a field name obscures a package name, then an
<code class="literal">import</code> declaration (<a class="xref" href="jls-7.html#jls-7.5" title="7.5.&nbsp;Import Declarations">&sect;7.5</a>) can usually be
used to make available the type names declared in that
package.
</p>
</li>
<li class="listitem">
<p class="note">If a field name obscures a type name, then a
fully qualified name for the type can be used unless the type
name denotes a local class (<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>).
</p>
</li>
<li class="listitem">
<p class="note">Field names cannot obscure method names.</p>
</li>
<li class="listitem">
<p class="note">If a field name is shadowed by a declaration of
a parameter or local variable, then the name of the parameter or
local variable can be changed without affecting other
code.
</p>
</li>
</ul>
</div>
<p class="note">Obscuring involving constant names is rare:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">Constant names normally have no lowercase
letters, so they will not normally obscure names of packages or
types, nor will they normally shadow fields, whose names
typically contain at least one lowercase letter.
</p>
</li>
<li class="listitem">
<p class="note">Constant names cannot obscure method names,
because they are distinguished syntactically.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section" title="6.5.&nbsp;Determining the Meaning of a Name">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-6.5"></a>6.5.&nbsp;Determining the Meaning of a Name
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.5-100"></a>The meaning
of a name depends on the context in which it is used. The
determination of the meaning of a name requires three steps:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5-100-A"></a>
First, context causes a name syntactically to fall into one of
six categories: <span class="emphasis"><em>PackageName</em></span>, <span class="emphasis"><em>TypeName</em></span>, <span class="emphasis"><em>ExpressionName</em></span>,
<span class="emphasis"><em>MethodName</em></span>, <span class="emphasis"><em>PackageOrTypeName</em></span>, or <span class="emphasis"><em>AmbiguousName</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5-100-B"></a>
Second, a name that is initially classified by its context as an
<span class="emphasis"><em>AmbiguousName</em></span> or as a <span class="emphasis"><em>PackageOrTypeName</em></span> is then reclassified
to be a <span class="emphasis"><em>PackageName</em></span>, <span class="emphasis"><em>TypeName</em></span>, or <span class="emphasis"><em>ExpressionName</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5-100-C"></a>
Third, the resulting category then dictates the final
determination of the meaning of the name (or a compile-time error
if the name has no meaning).
</p>
</li>
</ul>
</div>
<div id="jls-6.5-110" class="productionset"><a name="jls-6.5-110"></a>
<div class="production"><a name="jls-PackageName"></a>
<div class="lhs">PackageName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
<a href="jls-6.html#jls-PackageName" title="PackageName">PackageName</a> <code class="literal">.</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-TypeName"></a>
<div class="lhs">TypeName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
<a href="jls-6.html#jls-PackageOrTypeName" title="PackageOrTypeName">PackageOrTypeName</a> <code class="literal">.</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-PackageOrTypeName"></a>
<div class="lhs">PackageOrTypeName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
<a href="jls-6.html#jls-PackageOrTypeName" title="PackageOrTypeName">PackageOrTypeName</a> <code class="literal">.</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-ExpressionName"></a>
<div class="lhs">ExpressionName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
<a href="jls-6.html#jls-AmbiguousName" title="AmbiguousName">AmbiguousName</a> <code class="literal">.</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-MethodName"></a>
<div class="lhs">MethodName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
</div>
</div>
<div class="production"><a name="jls-AmbiguousName"></a>
<div class="lhs">AmbiguousName:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> <br>
<a href="jls-6.html#jls-AmbiguousName" title="AmbiguousName">AmbiguousName</a> <code class="literal">.</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
</div>
<p class="note">The use of context helps to minimize name conflicts
between entities of different kinds. Such conflicts will be rare if
the naming conventions described in <a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a> 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. It is always possible to distinguish between a method and a
field with the same name, since the context of a use always tells
whether a method is intended.
</p>
<div class="section" title="6.5.1.&nbsp;Syntactic Classification of a Name According to Context">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-6.5.1"></a>6.5.1.&nbsp;Syntactic Classification of a Name According to Context
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.5.1-110"></a>A name is
syntactically classified as a <span class="emphasis"><em>TypeName</em></span> in these contexts:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A"></a>
The first ten non-generic contexts
(<a class="xref" href="jls-6.html#jls-6.1" title="6.1.&nbsp;Declarations">&sect;6.1</a>):
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-A"></a>
In a single-type-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.1" title="7.5.1.&nbsp;Single-Type-Import Declarations">&sect;7.5.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-B"></a>
To the left of the <code class="literal">.</code> in a single-static-import
declaration (<a class="xref" href="jls-7.html#jls-7.5.3" title="7.5.3.&nbsp;Single-Static-Import Declarations">&sect;7.5.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-C"></a>
To the left of the <code class="literal">.</code> in a static-import-on-demand
declaration (<a class="xref" href="jls-7.html#jls-7.5.4" title="7.5.4.&nbsp;Static-Import-on-Demand Declarations">&sect;7.5.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-D"></a>
To the left of the <code class="literal">(</code> in a constructor declaration
(<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-E"></a>
After the <code class="literal">@</code> sign in an annotation
(<a class="xref" href="jls-9.html#jls-9.7" title="9.7.&nbsp;Annotations">&sect;9.7</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-F"></a>
To the left of <code class="literal">.</code><code class="literal">class</code> in a class literal
(<a class="xref" href="jls-15.html#jls-15.8.2" title="15.8.2.&nbsp;Class Literals">&sect;15.8.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-G"></a>
To the left of <code class="literal">.</code><code class="literal">this</code> in a qualified <code class="literal">this</code> expression
(<a class="xref" href="jls-15.html#jls-15.8.4" title="15.8.4.&nbsp;Qualified this">&sect;15.8.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-H"></a>
To the left of <code class="literal">.</code><code class="literal">super</code> in a qualified superclass field
access expression (<a class="xref" href="jls-15.html#jls-15.11.2" title="15.11.2.&nbsp;Accessing Superclass Members using super">&sect;15.11.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-I"></a>
To the left of <code class="literal">.</code><span class="emphasis"><em>Identifier</em></span> or
<code class="literal">.</code><code class="literal">super</code><code class="literal">.</code><span class="emphasis"><em>Identifier</em></span> in a qualified method
invocation expression (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-A-J"></a>
To the left of <code class="literal">.</code><code class="literal">super</code><code class="literal">::</code> in a method
reference expression (<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>)
</p>
</li>
</ol>
</div>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B"></a>
As the <span class="emphasis"><em>Identifier</em></span> or dotted <span class="emphasis"><em>Identifier</em></span> sequence that
constitutes any <span class="emphasis"><em>ReferenceType</em></span> (including a <span class="emphasis"><em>ReferenceType</em></span> to
the left of the brackets in an array type, or to the left of the
&lt; in a parameterized type, or in a non-wildcard type argument
of a parameterized type, or in an <code class="literal">extends</code> or <code class="literal">super</code> clause of
a wildcard type argument of a parameterized type) in the 16
contexts where types are used
(<a class="xref" href="jls-4.html#jls-4.11" title="4.11.&nbsp;Where Types Are Used">&sect;4.11</a>):
</p>
<div class="orderedlist">
<ol class="orderedlist" type="1">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-A"></a>
In an <code class="literal">extends</code> or <code class="literal">implements</code> clause of a class
declaration (<a class="xref" href="jls-8.html#jls-8.1.4" title="8.1.4.&nbsp;Superclasses and Subclasses">&sect;8.1.4</a>,
<a class="xref" href="jls-8.html#jls-8.1.5" title="8.1.5.&nbsp;Superinterfaces">&sect;8.1.5</a>, <a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>,
<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-B"></a>
In an <code class="literal">extends</code> clause of an interface declaration
(<a class="xref" href="jls-9.html#jls-9.1.3" title="9.1.3.&nbsp;Superinterfaces and Subinterfaces">&sect;9.1.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-C"></a>
The return type of a method (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>,
<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>) (including the type of an
element of an annotation type
(<a class="xref" href="jls-9.html#jls-9.6.1" title="9.6.1.&nbsp;Annotation Type Elements">&sect;9.6.1</a>))
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-D"></a>
In the <code class="literal">throws</code> clause of a method or constructor
(<a class="xref" href="jls-8.html#jls-8.4.6" title="8.4.6.&nbsp;Method Throws">&sect;8.4.6</a>, <a class="xref" href="jls-8.html#jls-8.8.5" title="8.8.5.&nbsp;Constructor Throws">&sect;8.8.5</a>,
<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-E"></a>
In an <code class="literal">extends</code> clause of a type parameter declaration of a
generic class, interface, method, or constructor
(<a class="xref" href="jls-8.html#jls-8.1.2" title="8.1.2.&nbsp;Generic Classes and Type Parameters">&sect;8.1.2</a>, <a class="xref" href="jls-9.html#jls-9.1.2" title="9.1.2.&nbsp;Generic Interfaces and Type Parameters">&sect;9.1.2</a>,
<a class="xref" href="jls-8.html#jls-8.4.4" title="8.4.4.&nbsp;Generic Methods">&sect;8.4.4</a>,
<a class="xref" href="jls-8.html#jls-8.8.4" title="8.8.4.&nbsp;Generic Constructors">&sect;8.8.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-F"></a>
The type in a field declaration of a class or interface
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>,
<a class="xref" href="jls-9.html#jls-9.3" title="9.3.&nbsp;Field (Constant) Declarations">&sect;9.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-G"></a>
The type in a formal parameter declaration of a method or
constructor (<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>,
<a class="xref" href="jls-8.html#jls-8.8.1" title="8.8.1.&nbsp;Formal Parameters">&sect;8.8.1</a>,
<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-H"></a>
The type of the receiver parameter of a method
(<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-I"></a>
The type in a local variable declaration
(<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>, <a class="xref" href="jls-14.html#jls-14.14.1" title="14.14.1.&nbsp;The basic for Statement">&sect;14.14.1</a>,
<a class="xref" href="jls-14.html#jls-14.14.2" title="14.14.2.&nbsp;The enhanced for statement">&sect;14.14.2</a>,
<a class="xref" href="jls-14.html#jls-14.20.3" title="14.20.3.&nbsp;try-with-resources">&sect;14.20.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-J"></a>
A type in an exception parameter declaration
(<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-K"></a>
In an explicit type argument list to an explicit
constructor invocation statement or class instance creation
expression or method invocation expression
(<a class="xref" href="jls-8.html#jls-8.8.7.1" title="8.8.7.1.&nbsp;Explicit Constructor Invocations">&sect;8.8.7.1</a>, <a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>,
<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-L"></a>
In an unqualified class instance creation expression,
either as the class type to be instantiated
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>) or as the direct superclass or
direct superinterface of an anonymous class to be
instantiated (<a class="xref" href="jls-15.html#jls-15.9.5" title="15.9.5.&nbsp;Anonymous Class Declarations">&sect;15.9.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-M"></a>
The element type in an array creation expression
(<a class="xref" href="jls-15.html#jls-15.10.1" title="15.10.1.&nbsp;Array Creation Expressions">&sect;15.10.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-N"></a>
The type in the cast operator of a cast expression
(<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-O"></a>
The type that follows the <code class="literal">instanceof</code> relational operator
(<a class="xref" href="jls-15.html#jls-15.20.2" title="15.20.2.&nbsp;Type Comparison Operator instanceof">&sect;15.20.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-110-B-P"></a>
In a method reference expression
(<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>), as the reference type to
search for a member method or as the class type or array
type to construct.
</p>
</li>
</ol>
</div>
</li>
</ul>
</div>
<div class="informalexample">
<p class="note">The extraction of a <span class="emphasis"><em>TypeName</em></span>
from the identifiers of a <span class="emphasis"><em>ReferenceType</em></span> in the 16 contexts above is
intended to apply recursively to all sub-terms of the <span class="emphasis"><em>ReferenceType</em></span>,
such as its element type and any type arguments.
</p>
<p class="note">For example, suppose a field declaration uses the
type <code class="literal">p.q.Foo[]</code>. The brackets of the array type are
ignored, and the term <code class="literal">p.q.Foo</code> is extracted as a
dotted sequence of <span class="emphasis"><em>Identifiers</em></span> to the left of the
brackets in an array type, and classified as a <span class="emphasis"><em>TypeName</em></span>. A later
step determines which of <code class="literal">p</code>, <code class="literal">q</code>,
and <code class="literal">Foo</code> is a type name or a package name.
</p>
<p class="note">As another example, suppose a cast operator uses the
type <code class="literal">p.q.Foo&lt;? extends String&gt;</code>. The term
<code class="literal">p.q.Foo</code> is again extracted as a dotted sequence of
<span class="emphasis"><em>Identifier</em></span> terms, this time to the left of the <code class="literal">&lt;</code> in a
parameterized type, and classified as a <span class="emphasis"><em>TypeName</em></span>. The term <code class="literal">String</code>
is extracted as an <span class="emphasis"><em>Identifier</em></span> in an <code class="literal">extends</code> clause of a wildcard
type argument of a parameterized type, and classified as a
<span class="emphasis"><em>TypeName</em></span>.
</p>
</div>
<p class="norm-static"><a name="jls-6.5.1-120"></a>A name is
syntactically classified as an <span class="emphasis"><em>ExpressionName</em></span> in
these contexts:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-A"></a>
As the qualifying expression in a qualified superclass
constructor invocation (<a class="xref" href="jls-8.html#jls-8.8.7.1" title="8.8.7.1.&nbsp;Explicit Constructor Invocations">&sect;8.8.7.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-B"></a>
As the qualifying expression in a qualified class instance
creation expression (<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-C"></a>
As the array reference expression in an array access expression
(<a class="xref" href="jls-15.html#jls-15.10.3" title="15.10.3.&nbsp;Array Access Expressions">&sect;15.10.3</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-D"></a>
As a <span class="emphasis"><em>PostfixExpression</em></span>
(<a class="xref" href="jls-15.html#jls-15.14" title="15.14.&nbsp;Postfix Expressions">&sect;15.14</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-E"></a>
As the left-hand operand of an assignment operator
(<a class="xref" href="jls-15.html#jls-15.26" title="15.26.&nbsp;Assignment Operators">&sect;15.26</a>)
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.5.1-130"></a>A name is
syntactically classified as a <span class="emphasis"><em>MethodName</em></span> in this context:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-130-A"></a>
Before the "<code class="literal">(</code>" in a method invocation expression
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>)
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.5.1-140"></a>A name is
syntactically classified as a <span class="emphasis"><em>PackageOrTypeName</em></span> in these
contexts:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-140-A"></a>
To the left of the "<code class="literal">.</code>" in a qualified <span class="emphasis"><em>TypeName</em></span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-140-B"></a>
In a type-import-on-demand declaration
(<a class="xref" href="jls-7.html#jls-7.5.2" title="7.5.2.&nbsp;Type-Import-on-Demand Declarations">&sect;7.5.2</a>)
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-6.5.1-150"></a>A name is
syntactically classified as an <span class="emphasis"><em>AmbiguousName</em></span> in these
contexts:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-150-A"></a>
To the left of the "<code class="literal">.</code>" in a qualified <span class="emphasis"><em>ExpressionName</em></span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-150-B"></a>
To the left of the rightmost <code class="literal">.</code> that occurs before the
"<code class="literal">(</code>" in a method invocation expression
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-150-C"></a>
To the left of the "<code class="literal">.</code>" in a qualified <span class="emphasis"><em>AmbiguousName</em></span></p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-150-D"></a>
In the default value clause of an annotation type element
declaration (<a class="xref" href="jls-9.html#jls-9.6.2" title="9.6.2.&nbsp;Defaults for Annotation Type Elements">&sect;9.6.2</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-150-E"></a>
To the right of an "<code class="literal">=</code>" in an an element-value pair
(<a class="xref" href="jls-9.html#jls-9.7.1" title="9.7.1.&nbsp;Normal Annotations">&sect;9.7.1</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.1-120-F"></a>
To the left of <code class="literal">::</code> in a method reference expression
(<a class="xref" href="jls-15.html#jls-15.13" title="15.13.&nbsp;Method Reference Expressions">&sect;15.13</a>)
</p>
</li>
</ul>
</div>
<p class="note">The effect of syntactic classification is to
restrict certain kinds of entities to certain parts of
expressions:
</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">The name of a field, parameter, or local
variable may be used as an expression
(<a class="xref" href="jls-15.html#jls-15.14.1" title="15.14.1.&nbsp;Expression Names">&sect;15.14.1</a>).
</p>
</li>
<li class="listitem">
<p class="note">The name of a method may appear in an expression
only as part of a method invocation expression
(<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>).
</p>
</li>
<li class="listitem">
<p class="note">The name of a class or interface type may appear
in an expression only as part of a class literal
(<a class="xref" href="jls-15.html#jls-15.8.2" title="15.8.2.&nbsp;Class Literals">&sect;15.8.2</a>), a qualified <code class="literal">this</code> expression
(<a class="xref" href="jls-15.html#jls-15.8.4" title="15.8.4.&nbsp;Qualified this">&sect;15.8.4</a>), a class instance creation
expression (<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>), an array creation
expression (<a class="xref" href="jls-15.html#jls-15.10.1" title="15.10.1.&nbsp;Array Creation Expressions">&sect;15.10.1</a>), a cast expression
(<a class="xref" href="jls-15.html#jls-15.16" title="15.16.&nbsp;Cast Expressions">&sect;15.16</a>), an <code class="literal">instanceof</code> expression
(<a class="xref" href="jls-15.html#jls-15.20.2" title="15.20.2.&nbsp;Type Comparison Operator instanceof">&sect;15.20.2</a>), an enum constant
(<a class="xref" href="jls-8.html#jls-8.9" title="8.9.&nbsp;Enum Types">&sect;8.9</a>), or as part of a qualified name for
a field or method.
</p>
</li>
<li class="listitem">
<p class="note">The name of a package may appear in an
expression only as part of a qualified name for a class or
interface type.
</p>
</li>
</ul>
</div>
</div>
<div class="section" title="6.5.2.&nbsp;Reclassification of Contextually Ambiguous Names">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-6.5.2"></a>6.5.2.&nbsp;Reclassification of Contextually Ambiguous Names
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-6.5.2-100"></a>An
<span class="emphasis"><em>AmbiguousName</em></span> is then reclassified as follows.
</p>
<p class="norm-static"><a name="jls-6.5.2-110"></a>If the
<span class="emphasis"><em>AmbiguousName</em></span> is a simple name, consisting of a single
<span class="emphasis"><em>Identifier</em></span>:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.2-110-A"></a>
If the <span class="emphasis"><em>Identifier</em></span> appears within the scope
(<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>) of a local variable declaration
(<a class="xref" href="jls-14.html#jls-14.4" title="14.4.&nbsp;Local Variable Declaration Statements">&sect;14.4</a>) or parameter declaration
(<a class="xref" href="jls-8.html#jls-8.4.1" title="8.4.1.&nbsp;Formal Parameters">&sect;8.4.1</a>, <a class="xref" href="jls-8.html#jls-8.8.1" title="8.8.1.&nbsp;Formal Parameters">&sect;8.8.1</a>,
<a class="xref" href="jls-14.html#jls-14.20" title="14.20.&nbsp;The try statement">&sect;14.20</a>) or field declaration
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>) with that name, then the
<span class="emphasis"><em>AmbiguousName</em></span> is reclassified as an <span class="emphasis"><em>ExpressionName</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.2-110-B"></a>
Otherwise, if a field of that name is declared in the
compilation unit (<a class="xref" href="jls-7.html#jls-7.3" title="7.3.&nbsp;Compilation Units">&sect;7.3</a>) containing the
<span class="emphasis"><em>Identifier</em></span> by a single-static-import declaration
(<a class="xref" href="jls-7.html#jls-7.5.3" title="7.5.3.&nbsp;Single-Static-Import Declarations">&sect;7.5.3</a>), or by a static-import-on-demand
declaration (<a class="xref" href="jls-7.html#jls-7.5.4" title="7.5.4.&nbsp;Static-Import-on-Demand Declarations">&sect;7.5.4</a>) then the
<span class="emphasis"><em>AmbiguousName</em></span> is reclassified as an <span class="emphasis"><em>ExpressionName</em></span>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-6.5.2-110-C"></a>
Otherwise, if the <span class="emphasis"><em>Identifier</em></span> appears within the scope
(<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a>) of a top level class
(<a class="xref" href="jls-8.html" title="Chapter&nbsp;8.&nbsp;Classes">&sect;8 (<i>Classes</i>)</a>) or interface type declaration
(<a class="xref" href="jls-9.html" title="Chapter&nbsp;9.&nbsp;Interfaces">&sect;9 (<i>Interfaces</i>)</a>), a local class declaration
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>) or member type declaration
(<a class="xref"