Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
7234 lines (6744 sloc) 450 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;8.&nbsp;Classes</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-7.html" title="Chapter&nbsp;7.&nbsp;Packages">
<link rel="next" href="jls-9.html" title="Chapter&nbsp;9.&nbsp;Interfaces">
<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;8.&nbsp;Classes</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="jls-7.html">Prev</a>&nbsp;
</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="jls-9.html">Next</a></td>
</tr>
</table>
<hr>
</div>
<div lang="en" class="chapter" title="Chapter&nbsp;8.&nbsp;Classes">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="jls-8"></a>Chapter&nbsp;8.&nbsp;Classes
</h2>
</div>
</div>
</div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.1">8.1. Class Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.1.1">8.1.1. Class Modifiers</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.1.1.1">8.1.1.1. <code class="literal">abstract</code> Classes</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.1.2">8.1.1.2. <code class="literal">final</code> Classes</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.1.3">8.1.1.3. <code class="literal">strictfp</code> Classes</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.1.2">8.1.2. Generic Classes and Type Parameters</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.3">8.1.3. Inner Classes and Enclosing Instances</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.4">8.1.4. Superclasses and Subclasses</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.5">8.1.5. Superinterfaces</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.1.6">8.1.6. Class Body and Member Declarations</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.2">8.2. Class Members</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.3">8.3. Field Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.3.1">8.3.1. Field Modifiers</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.3.1.1">8.3.1.1. <code class="literal">static</code> Fields</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.3.1.2">8.3.1.2. <code class="literal">final</code> Fields</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.3.1.3">8.3.1.3. <code class="literal">transient</code> Fields</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.3.1.4">8.3.1.4. <code class="literal">volatile</code> Fields</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.3.2">8.3.2. Field Initialization</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.3.3">8.3.3. Forward References During Field Initialization</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.4">8.4. Method Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.4.1">8.4.1. Formal Parameters</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.2">8.4.2. Method Signature</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3">8.4.3. Method Modifiers</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.1">8.4.3.1. <code class="literal">abstract</code> Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.2">8.4.3.2. <code class="literal">static</code> Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.3">8.4.3.3. <code class="literal">final</code> Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.4">8.4.3.4. <code class="literal">native</code> Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.5">8.4.3.5. <code class="literal">strictfp</code> Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.3.6">8.4.3.6. <code class="literal">synchronized</code> Methods</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.4.4">8.4.4. Generic Methods</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.5">8.4.5. Method Result</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.6">8.4.6. Method Throws</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.7">8.4.7. Method Body</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.8">8.4.8. Inheritance, Overriding, and Hiding</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.4.8.1">8.4.8.1. Overriding (by Instance Methods)</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.8.2">8.4.8.2. Hiding (by Class Methods)</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.8.3">8.4.8.3. Requirements in Overriding and Hiding</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.4.8.4">8.4.8.4. Inheriting Methods with Override-Equivalent Signatures</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.4.9">8.4.9. Overloading</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.5">8.5. Member Type Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.5.1">8.5.1. Static Member Type Declarations</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.6">8.6. Instance Initializers</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.7">8.7. Static Initializers</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8">8.8. Constructor Declarations</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.8.1">8.8.1. Formal Parameters</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.2">8.8.2. Constructor Signature</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.3">8.8.3. Constructor Modifiers</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.4">8.8.4. Generic Constructors</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.5">8.8.5. Constructor Throws</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.6">8.8.6. The Type of a Constructor</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.7">8.8.7. Constructor Body</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.8.7.1">8.8.7.1. Explicit Constructor Invocations</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.8.8">8.8.8. Constructor Overloading</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.9">8.8.9. Default Constructor</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.8.10">8.8.10. Preventing Instantiation of a Class</a></span></dt>
</dl>
</dd>
<dt><span class="section"><a href="jls-8.html#jls-8.9">8.9. Enum Types</a></span></dt>
<dd>
<dl>
<dt><span class="section"><a href="jls-8.html#jls-8.9.1">8.9.1. Enum Constants</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.9.2">8.9.2. Enum Body Declarations</a></span></dt>
<dt><span class="section"><a href="jls-8.html#jls-8.9.3">8.9.3. Enum Members</a></span></dt>
</dl>
</dd>
</dl>
</div>
<p class="norm"><a name="jls-8-100"></a>Class declarations define new
reference types and describe how they are implemented
(<a class="xref" href="jls-8.html#jls-8.1" title="8.1.&nbsp;Class Declarations">&sect;8.1</a>).
</p>
<p class="norm"><a name="jls-8-110"></a>A <span class="emphasis"><em>top level
class</em></span> is a class that is not a nested class.
</p>
<p class="norm"><a name="jls-8-120"></a>A <span class="emphasis"><em>nested
class</em></span> is any class whose declaration occurs within the body
of another class or interface.
</p>
<p class="norm"><a name="jls-8-130"></a>This chapter discusses the common
semantics of all classes - top level (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) and
nested (including member classes (<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>), 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.5" title="15.9.5.&nbsp;Anonymous Class Declarations">&sect;15.9.5</a>)). Details that
are specific to particular kinds of classes are discussed in the
sections dedicated to these constructs.
</p>
<p class="norm"><a name="jls-8-200"></a>A named class may be declared
<code class="literal">abstract</code> (<a class="xref" href="jls-8.html#jls-8.1.1.1" title="8.1.1.1.&nbsp;abstract Classes">&sect;8.1.1.1</a>) and must be declared
abstract if it is incompletely implemented; such a class cannot be
instantiated, but can be extended by subclasses. A class may be
declared <code class="literal">final</code> (<a class="xref" href="jls-8.html#jls-8.1.1.2" title="8.1.1.2.&nbsp;final Classes">&sect;8.1.1.2</a>), in which case it
cannot have subclasses. If a class is declared <code class="literal">public</code>, then it can
be referred to from other packages. Each class except <code class="literal">Object</code> is an
extension of (that is, a subclass of) a single existing class
(<a class="xref" href="jls-8.html#jls-8.1.4" title="8.1.4.&nbsp;Superclasses and Subclasses">&sect;8.1.4</a>) and may implement interfaces
(<a class="xref" href="jls-8.html#jls-8.1.5" title="8.1.5.&nbsp;Superinterfaces">&sect;8.1.5</a>). Classes may
be <span class="emphasis"><em>generic</em></span> (<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
is, they may declare type variables whose bindings may differ among
different instances of the class.
</p>
<p class="norm"><a name="jls-8-210"></a>Classes may be decorated with
annotations (<a class="xref" href="jls-9.html#jls-9.7" title="9.7.&nbsp;Annotations">&sect;9.7</a>) just like any other kind of
declaration.
</p>
<p class="norm"><a name="jls-8-300"></a>The body of a class declares
members (fields and methods and nested classes and interfaces),
instance and static initializers, and constructors
(<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>). 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 member (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>) is the entire body of the
declaration of the class to which the member belongs. Field, method,
member class, member interface, and constructor declarations may
include the access modifiers (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) <code class="literal">public</code>,
<code class="literal">protected</code>, or <code class="literal">private</code>. The members of a class include both
declared and inherited members (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>). Newly
declared fields can hide fields declared in a superclass or
superinterface. Newly declared class members and interface members can
hide class or interface members declared in a superclass or
superinterface. Newly declared methods can hide, implement, or
override methods declared in a superclass or superinterface.
</p>
<p class="norm"><a name="jls-8-310"></a>Field declarations
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>) describe class variables, which are
incarnated once, and instance variables, which are freshly incarnated
for each instance of the class. A field may be declared <code class="literal">final</code>
(<a class="xref" href="jls-8.html#jls-8.3.1.2" title="8.3.1.2.&nbsp;final Fields">&sect;8.3.1.2</a>), in which case it can be assigned to
only once. Any field declaration may include an initializer.
</p>
<p class="norm"><a name="jls-8-320"></a>Member class declarations
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>) describe nested classes that are members
of the surrounding class. Member classes may be <code class="literal">static</code>, in which
case they have no access to the instance variables of the surrounding
class; or they may be inner classes
(<a class="xref" href="jls-8.html#jls-8.1.3" title="8.1.3.&nbsp;Inner Classes and Enclosing Instances">&sect;8.1.3</a>).
</p>
<p class="norm"><a name="jls-8-330"></a>Member interface declarations
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>) describe nested interfaces that are
members of the surrounding class.
</p>
<p class="norm"><a name="jls-8-340"></a>Method declarations
(<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>) describe code that may be invoked by
method invocation expressions (<a class="xref" href="jls-15.html#jls-15.12" title="15.12.&nbsp;Method Invocation Expressions">&sect;15.12</a>). A class
method is invoked relative to the class type; an instance method is
invoked with respect to some particular object that is an instance of
a class type. A method whose declaration does not indicate how it is
implemented must be declared <code class="literal">abstract</code>. A method may be declared
<code class="literal">final</code> (<a class="xref" href="jls-8.html#jls-8.4.3.3" title="8.4.3.3.&nbsp;final Methods">&sect;8.4.3.3</a>), in which case it cannot be
hidden or overridden. A method may be implemented by
platform-dependent <code class="literal">native</code> code (<a class="xref" href="jls-8.html#jls-8.4.3.4" title="8.4.3.4.&nbsp;native Methods">&sect;8.4.3.4</a>). A
<code class="literal">synchronized</code> method (<a class="xref" href="jls-8.html#jls-8.4.3.6" title="8.4.3.6.&nbsp;synchronized Methods">&sect;8.4.3.6</a>) automatically
locks an object before executing its body and automatically unlocks
the object on return, as if by use of a <code class="literal">synchronized</code> statement
(<a class="xref" href="jls-14.html#jls-14.19" title="14.19.&nbsp;The synchronized Statement">&sect;14.19</a>), thus allowing its activities to be
synchronized with those of other threads
(<a class="xref" href="jls-17.html" title="Chapter&nbsp;17.&nbsp;Threads and Locks">&sect;17 (<i>Threads and Locks</i>)</a>).
</p>
<p class="norm"><a name="jls-8-350"></a>Method names may be overloaded
(<a class="xref" href="jls-8.html#jls-8.4.9" title="8.4.9.&nbsp;Overloading">&sect;8.4.9</a>).
</p>
<p class="norm"><a name="jls-8-400"></a>Instance initializers
(<a class="xref" href="jls-8.html#jls-8.6" title="8.6.&nbsp;Instance Initializers">&sect;8.6</a>) are blocks of executable code that may be
used to help initialize an instance when it is created
(<a class="xref" href="jls-15.html#jls-15.9" title="15.9.&nbsp;Class Instance Creation Expressions">&sect;15.9</a>).
</p>
<p class="norm"><a name="jls-8-410"></a>Static initializers
(<a class="xref" href="jls-8.html#jls-8.7" title="8.7.&nbsp;Static Initializers">&sect;8.7</a>) are blocks of executable code that may be
used to help initialize a class.
</p>
<p class="norm"><a name="jls-8-420"></a>Constructors
(<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</a>) are similar to methods, but cannot be
invoked directly by a method call; they are used to initialize new
class instances. Like methods, they may be overloaded
(<a class="xref" href="jls-8.html#jls-8.8.8" title="8.8.8.&nbsp;Constructor Overloading">&sect;8.8.8</a>).
</p>
<div class="section" title="8.1.&nbsp;Class Declarations">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-8.1"></a>8.1.&nbsp;Class Declarations
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1-100"></a>A class
declaration specifies a new named reference type.
</p>
<p class="norm-static"><a name="jls-8.1-110"></a>There are
two kinds of class declarations: <span class="emphasis"><em>normal class
declarations</em></span> and <span class="emphasis"><em>enum
declarations</em></span>.
</p>
<div id="jls-8.1-120" class="productionset"><a name="jls-8.1-120"></a>
<div class="production"><a name="jls-ClassDeclaration"></a>
<div class="lhs">ClassDeclaration:</div>
<div class="rhs">
<a href="jls-8.html#jls-NormalClassDeclaration" title="NormalClassDeclaration">NormalClassDeclaration</a> <br>
<a href="jls-8.html#jls-EnumDeclaration" title="EnumDeclaration">EnumDeclaration</a>
</div>
</div>
<div class="production"><a name="jls-NormalClassDeclaration"></a>
<div class="lhs">NormalClassDeclaration:</div>
<div class="rhs">
{<a href="jls-8.html#jls-ClassModifier" title="ClassModifier">ClassModifier</a>}
<code class="literal">class</code> <a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-8.html#jls-TypeParameters" title="TypeParameters">TypeParameters</a>]
[<a href="jls-8.html#jls-Superclass" title="Superclass">Superclass</a>]
[<a href="jls-8.html#jls-Superinterfaces" title="Superinterfaces">Superinterfaces</a>]
<a href="jls-8.html#jls-ClassBody" title="ClassBody">ClassBody</a>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1-130"></a>The rules in this section apply
to all class declarations, including enum declarations. However,
special rules apply to enum declarations with regard to class
modifiers, inner classes, and superclasses; these rules are stated in
<a class="xref" href="jls-8.html#jls-8.9" title="8.9.&nbsp;Enum Types">&sect;8.9</a>.
</p>
<p class="norm-static"><a name="jls-8.1-200"></a>The
<span class="emphasis"><em>Identifier</em></span> in a class declaration specifies the name of the
class.
</p>
<p class="norm-error"><a name="jls-8.1-210"></a>It is a
compile-time error if a class has the same simple name as any of its
enclosing classes or interfaces.
</p>
<p class="norm-static"><a name="jls-8.1-300"></a>The scope and shadowing of a class
declaration is specified in <a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a> and
<a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<div class="section" title="8.1.1.&nbsp;Class Modifiers">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.1"></a>8.1.1.&nbsp;Class Modifiers
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1.1-100"></a>A class declaration may
include <span class="emphasis"><em>class modifiers</em></span>.
</p>
<div id="jls-8.1.1-110" class="productionset"><a name="jls-8.1.1-110"></a>
<div class="production"><a name="jls-ClassModifier"></a>
<div class="lhs">ClassModifier:</div>
<div class="rhs">
<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a> <code class="literal">public</code> <code class="literal">protected</code> <code class="literal">private</code> <br>
<code class="literal">abstract</code> <code class="literal">static</code> <code class="literal">final</code> <code class="literal">strictfp</code>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-8.1.1-120"></a>The rules for
annotation modifiers on a class declaration are specified in
<a class="xref" href="jls-9.html#jls-9.7.4" title="9.7.4.&nbsp;Where Annotations May Appear">&sect;9.7.4</a> and <a class="xref" href="jls-9.html#jls-9.7.5" title="9.7.5.&nbsp;Multiple Annotations Of The Same Type">&sect;9.7.5</a>.
</p>
<p class="norm-static"><a name="jls-8.1.1-200"></a>The access
modifier <code class="literal">public</code> (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>) pertains only to top
level classes (<a class="xref" href="jls-7.html#jls-7.6" title="7.6.&nbsp;Top Level Type Declarations">&sect;7.6</a>) and member classes
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>), not to local classes
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>) or anonymous classes
(<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>
<p class="norm-static"><a name="jls-8.1.1-210"></a>The access
modifiers <code class="literal">protected</code> and <code class="literal">private</code> pertain only to member classes
within a directly enclosing class declaration
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>).
</p>
<p class="norm-static"><a name="jls-8.1.1-220"></a>The
modifier <code class="literal">static</code> pertains only to member classes
(<a class="xref" href="jls-8.html#jls-8.5.1" title="8.5.1.&nbsp;Static Member Type Declarations">&sect;8.5.1</a>), not to top level or local or anonymous
classes.
</p>
<p class="norm-error"><a name="jls-8.1.1-300"></a>It is a
compile-time error if the same keyword appears more than once as a
modifier for a class declaration.
</p>
<p class="note">If two or more (distinct) class modifiers appear in
a class declaration, then it is customary, though not required, that
they appear in the order consistent with that shown above in the
production for <span class="emphasis"><em>ClassModifier</em></span>.
</p>
<div class="section" title="8.1.1.1.&nbsp;abstract Classes">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-8.1.1.1"></a>8.1.1.1.&nbsp;<code class="literal">abstract</code> Classes
</h4>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1.1.1-100"></a>An <code class="literal">abstract</code> class is a
class that is incomplete, or to be considered incomplete.
</p>
<p class="norm"><a name="jls-8.1.1.1-110"></a>It is a compile-time error
if an attempt is made to create an instance of an <code class="literal">abstract</code> class
using a class instance creation expression
(<a class="xref" href="jls-15.html#jls-15.9.1" title="15.9.1.&nbsp;Determining the Class being Instantiated">&sect;15.9.1</a>).
</p>
<p class="norm-dynamic"><a name="jls-8.1.1.1-120"></a>A
subclass of an <code class="literal">abstract</code> class that is not itself <code class="literal">abstract</code> may be
instantiated, resulting in the execution of a constructor for the
<code class="literal">abstract</code> class and, therefore, the execution of the field
initializers for instance variables of that class.
</p>
<p class="norm-error"><a name="jls-8.1.1.1-200"></a>A normal
class may have <code class="literal">abstract</code> methods, that is, methods that are declared
but not yet implemented (<a class="xref" href="jls-8.html#jls-8.4.3.1" title="8.4.3.1.&nbsp;abstract Methods">&sect;8.4.3.1</a>), only if it is
an <code class="literal">abstract</code> class. It is a compile-time error if a normal class that
is not <code class="literal">abstract</code> has an <code class="literal">abstract</code> method.
</p>
<p class="norm"><a name="jls-8.1.1.1-210"></a>A class <span class="type">C</span> has <code class="literal">abstract</code> methods if
either of the following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-8.1.1.1-210-A"></a>
Any of the member methods (<a class="xref" href="jls-8.html#jls-8.2" title="8.2.&nbsp;Class Members">&sect;8.2</a>) of <span class="type">C</span> -
either declared or inherited - is <code class="literal">abstract</code>.
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-8.1.1.1-210-B"></a>
Any of <span class="type">C</span>'s superclasses has an <code class="literal">abstract</code> method declared with
package access, and there exists no method that overrides the
<code class="literal">abstract</code> method from <span class="type">C</span> or from a superclass of <span class="type">C</span>.
</p>
</li>
</ul>
</div>
<p class="norm-error"><a name="jls-8.1.1.1-400"></a>It is a
compile-time error to declare an <code class="literal">abstract</code> class type such that it is
not possible to create a subclass that implements all of its
<code class="literal">abstract</code> methods. This situation can occur if the class would have
as members two <code class="literal">abstract</code> methods that have the same method signature
(<a class="xref" href="jls-8.html#jls-8.4.2" title="8.4.2.&nbsp;Method Signature">&sect;8.4.2</a>) but return types for which no type is
return-type-substitutable with both
(<a class="xref" href="jls-8.html#jls-8.4.5" title="8.4.5.&nbsp;Method Result">&sect;8.4.5</a>).
</p>
<div class="example"><a name="d5e10918"></a><p class="title"><b>Example&nbsp;8.1.1.1-1.&nbsp;Abstract Class Declaration</b></p>
<div class="example-contents"><pre class="programlisting">
abstract class Point {
int x = 1, y = 1;
void move(int dx, int dy) {
x += dx;
y += dy;
alert();
}
abstract void alert();
}
abstract class ColoredPoint extends Point {
int color;
}
class SimplePoint extends Point {
void alert() { }
}
</pre><p class="note">Here, a class <code class="literal">Point</code> is declared
that must be declared <code class="literal">abstract</code>, because it contains a declaration of
an <code class="literal">abstract</code> method named <code class="literal">alert</code>. The subclass
of <code class="literal">Point</code> named <code class="literal">ColoredPoint</code>
inherits the <code class="literal">abstract</code> method <code class="literal">alert</code>, so it must
also be declared <code class="literal">abstract</code>. On the other hand, the subclass
of <code class="literal">Point</code> named <code class="literal">SimplePoint</code>
provides an implementation of <code class="literal">alert</code>, so it need not
be <code class="literal">abstract</code>.
</p>
<p class="note">The statement:</p><pre class="screen">
Point p = new Point();
</pre><p class="note">would result in a compile-time error; the
class <code class="literal">Point</code> cannot be instantiated because it is
<code class="literal">abstract</code>. However, a <code class="literal">Point</code> variable could
correctly be initialized with a reference to any subclass
of <code class="literal">Point</code>, and the
class <code class="literal">SimplePoint</code> is not <code class="literal">abstract</code>, so the
statement:
</p><pre class="screen">
Point p = new SimplePoint();
</pre><p class="note">would be correct. Instantiation of
a <code class="literal">SimplePoint</code> causes the default constructor and
field initializers for <code class="literal">x</code> and <code class="literal">y</code>
of <code class="literal">Point</code> to be executed.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e10950"></a><p class="title"><b>Example&nbsp;8.1.1.1-2.&nbsp;Abstract Class Declaration that Prohibits Subclasses</b></p>
<div class="example-contents"><pre class="programlisting">
interface Colorable {
void setColor(int color);
}
abstract class Colored implements Colorable {
public abstract int setColor(int color);
}
</pre><p class="note">These declarations result in a compile-time error:
it would be impossible for any subclass of
class <code class="literal">Colored</code> to provide an implementation of a
method named <code class="literal">setColor</code>, taking one argument of type
<code class="literal">int</code>, that can satisfy both abstract method specifications, because
the one in interface <code class="literal">Colorable</code> requires the same
method to return no value, while the one in
class <code class="literal">Colored</code> requires the same method to return a
value of type <code class="literal">int</code> (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>).
</p>
</div>
</div><br class="example-break"><p class="note">A class type should be declared <code class="literal">abstract</code> only if
the intent is that subclasses can be created to complete the
implementation. If the intent is simply to prevent instantiation of a
class, the proper way to express this is to declare a constructor
(<a class="xref" href="jls-8.html#jls-8.8.10" title="8.8.10.&nbsp;Preventing Instantiation of a Class">&sect;8.8.10</a>) of no arguments, make it <code class="literal">private</code>,
never invoke it, and declare no other constructors. A class of this
form usually contains class methods and variables.
</p>
<div class="informalexample">
<p class="note">The class <code class="literal">Math</code> is an example of a
class that cannot be instantiated; its declaration looks like
this:
</p><pre class="programlisting">
public final class Math {
private Math() { } // never instantiate this class
. . . declarations of class variables and methods . . .
}
</pre></div>
</div>
<div class="section" title="8.1.1.2.&nbsp;final Classes">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-8.1.1.2"></a>8.1.1.2.&nbsp;<code class="literal">final</code> Classes
</h4>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1.1.2-100"></a>A class can be declared
<code class="literal">final</code> if its definition is complete and no subclasses are desired or
required.
</p>
<p class="norm-error"><a name="jls-8.1.1.2-200"></a>It is a
compile-time error if the name of a <code class="literal">final</code> class appears in the
<code class="literal">extends</code> clause (<a class="xref" href="jls-8.html#jls-8.1.4" title="8.1.4.&nbsp;Superclasses and Subclasses">&sect;8.1.4</a>) of another class
declaration; this implies that a <code class="literal">final</code> class cannot have any
subclasses.
</p>
<p class="norm-error"><a name="jls-8.1.1.2-210"></a>It is a
compile-time error if a class is declared both <code class="literal">final</code> and <code class="literal">abstract</code>,
because the implementation of such a class could never be completed
(<a class="xref" href="jls-8.html#jls-8.1.1.1" title="8.1.1.1.&nbsp;abstract Classes">&sect;8.1.1.1</a>).
</p>
<p class="norm-static"><a name="jls-8.1.1.2-300"></a>Because
a <code class="literal">final</code> class never has any subclasses, the methods of a <code class="literal">final</code>
class are never overridden (<a class="xref" href="jls-8.html#jls-8.4.8.1" title="8.4.8.1.&nbsp;Overriding (by Instance Methods)">&sect;8.4.8.1</a>).
</p>
</div>
<div class="section" title="8.1.1.3.&nbsp;strictfp Classes">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-8.1.1.3"></a>8.1.1.3.&nbsp;<code class="literal">strictfp</code> Classes
</h4>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1.1.3-100"></a>The
effect of the <code class="literal">strictfp</code> modifier is to make all <code class="literal">float</code> or <code class="literal">double</code>
expressions within the class declaration (including within variable
initializers, instance initializers, static initializers, and
constructors) be explicitly FP-strict
(<a class="xref" href="jls-15.html#jls-15.4" title="15.4.&nbsp;FP-strict Expressions">&sect;15.4</a>).
</p>
<p class="norm-static"><a name="jls-8.1.1.3-110"></a>This
implies that all methods declared in the class, and all nested types
declared in the class, are implicitly <code class="literal">strictfp</code>.
</p>
</div>
</div>
<div class="section" title="8.1.2.&nbsp;Generic Classes and Type Parameters">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.2"></a>8.1.2.&nbsp;Generic Classes and Type Parameters
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1.2-100"></a>A class
is <span class="emphasis"><em>generic</em></span> if it declares one or more type
variables (<a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a>).
</p>
<p class="norm"><a name="jls-8.1.2-110"></a>These type variables are
known as the <span class="emphasis"><em>type parameters</em></span> of the class. The
type parameter section follows the class name and is delimited by
angle brackets.
</p>
<div id="jls-8.1.2-120" class="productionset"><a name="jls-8.1.2-120"></a>
<div class="production"><a name="jls-TypeParameters"></a>
<div class="lhs">TypeParameters:</div>
<div class="rhs">
<code class="literal">&lt;</code> <a href="jls-8.html#jls-TypeParameterList" title="TypeParameterList">TypeParameterList</a> <code class="literal">&gt;</code>
</div>
</div>
<div class="production"><a name="jls-TypeParameterList"></a>
<div class="lhs">TypeParameterList:</div>
<div class="rhs">
<a href="jls-4.html#jls-TypeParameter" title="TypeParameter">TypeParameter</a> {<code class="literal">,</code> <a href="jls-4.html#jls-TypeParameter" title="TypeParameter">TypeParameter</a>}
</div>
</div>
</div>
<p class="note">The following productions from
<a class="xref" href="jls-4.html#jls-4.4" title="4.4.&nbsp;Type Variables">&sect;4.4</a> are shown here for convenience:
</p>
<div id="d5e11019" class="productionset"><a name="d5e11019"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">TypeParameter:</div>
<div class="rhs">
{<a href="jls-4.html#jls-TypeParameterModifier" title="TypeParameterModifier">TypeParameterModifier</a>}
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeBound" title="TypeBound">TypeBound</a>]
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">TypeParameterModifier:</div>
<div class="rhs">
<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">TypeBound:</div>
<div class="rhs">
<code class="literal">extends</code> <a href="jls-4.html#jls-TypeVariable" title="TypeVariable">TypeVariable</a> <br>
<code class="literal">extends</code> <a href="jls-4.html#jls-ClassOrInterfaceType" title="ClassOrInterfaceType">ClassOrInterfaceType</a> {<a href="jls-4.html#jls-AdditionalBound" title="AdditionalBound">AdditionalBound</a>}
</div>
</div>
</div>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">AdditionalBound:</div>
<div class="rhs">
<code class="literal">&amp;</code> <a href="jls-4.html#jls-InterfaceType" title="InterfaceType">InterfaceType</a>
</div>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-8.1.2-130"></a>The rules for
annotation modifiers on a type parameter declaration are specified in
<a class="xref" href="jls-9.html#jls-9.7.4" title="9.7.4.&nbsp;Where Annotations May Appear">&sect;9.7.4</a> and <a class="xref" href="jls-9.html#jls-9.7.5" title="9.7.5.&nbsp;Multiple Annotations Of The Same Type">&sect;9.7.5</a>.
</p>
<p class="norm"><a name="jls-8.1.2-200"></a>In a class's type parameter
section, a type variable <span class="type">T</span> <span class="emphasis"><em>directly depends</em></span> on
a type variable <span class="type">S</span> if <span class="type">S</span> is the bound of <span class="type">T</span>, while
<span class="type">T</span> <span class="emphasis"><em>depends</em></span> on <span class="type">S</span> if either <span class="type">T</span> directly depends
on <span class="type">S</span> or <span class="type">T</span> directly depends on a type variable <span class="type">U</span> that depends on
<span class="type">S</span> (using this definition recursively). It is a compile-time error if
a type variable in a class's type parameter section depends on
itself.
</p>
<p class="norm-static"><a name="jls-8.1.2-210"></a>The scope
and shadowing of a class's type parameter is specified in
<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a> and <a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<p class="norm-static"><a name="jls-8.1.2-300"></a>A generic
class declaration defines a set of parameterized types
(<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>), one for each possible parameterization of
the type parameter section by type arguments. All of these
parameterized types share the same class at run time.
</p>
<div class="informalexample">
<p class="note">For instance, executing the code:</p><pre class="programlisting">
Vector&lt;String&gt; x = new Vector&lt;String&gt;();
Vector&lt;Integer&gt; y = new Vector&lt;Integer&gt;();
boolean b = x.getClass() == y.getClass();
</pre><p class="note">will result in the variable <code class="literal">b</code>
holding the value <code class="literal">true</code>.
</p>
</div>
<p class="norm-error"><a name="jls-8.1.2-310"></a>It is a
compile-time error if a generic class is a direct or indirect subclass
of <code class="literal">Throwable</code> (<a class="xref" href="jls-11.html#jls-11.1.1" title="11.1.1.&nbsp;The Kinds of Exceptions">&sect;11.1.1</a>).
</p>
<p class="note">This restriction is needed since the catch mechanism
of the Java Virtual Machine works only with non-generic classes.
</p>
<p class="norm-error"><a name="jls-8.1.2-320"></a>It is a
compile-time error to refer to a type parameter of a generic class <span class="type">C</span>
in any of the following:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-8.1.2-320-A"></a>the declaration of a
<code class="literal">static</code> member of <span class="type">C</span> (<a class="xref" href="jls-8.html#jls-8.3.1.1" title="8.3.1.1.&nbsp;static Fields">&sect;8.3.1.1</a>,
<a class="xref" href="jls-8.html#jls-8.4.3.2" title="8.4.3.2.&nbsp;static Methods">&sect;8.4.3.2</a>,
<a class="xref" href="jls-8.html#jls-8.5.1" title="8.5.1.&nbsp;Static Member Type Declarations">&sect;8.5.1</a>).
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.2-320-B"></a>the declaration of a
<code class="literal">static</code> member of any type declaration nested within
<span class="type">C</span>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.2-320-C"></a>a static initializer of
<span class="type">C</span> (<a class="xref" href="jls-8.html#jls-8.7" title="8.7.&nbsp;Static Initializers">&sect;8.7</a>), or
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.2-320-D"></a>a static initializer of
any class declaration nested within <span class="type">C</span>.
</p>
</li>
</ul>
</div>
<div class="example"><a name="d5e11077"></a><p class="title"><b>Example&nbsp;8.1.2-1.&nbsp;Mutually Recursive Type Variable Bounds</b></p>
<div class="example-contents"><pre class="programlisting">
interface ConvertibleTo&lt;T&gt; {
T convert();
}
class ReprChange&lt;T extends ConvertibleTo&lt;S&gt;,
S extends ConvertibleTo&lt;T&gt;&gt; {
T t;
void set(S s) { t = s.convert(); }
S get() { return t.convert(); }
}
</pre></div>
</div><br class="example-break"><div class="example"><a name="d5e11080"></a><p class="title"><b>Example&nbsp;8.1.2-2.&nbsp;Nested Generic Classes</b></p>
<div class="example-contents"><pre class="programlisting">
class Seq&lt;T&gt; {
T head;
Seq&lt;T&gt; tail;
Seq() { this(null, null); }
Seq(T head, Seq&lt;T&gt; tail) {
this.head = head;
this.tail = tail;
}
boolean isEmpty() { return tail == null; }
class Zipper&lt;S&gt; {
Seq&lt;Pair&lt;T,S&gt;&gt; zip(Seq&lt;S&gt; that) {
if (isEmpty() || that.isEmpty()) {
return new Seq&lt;Pair&lt;T,S&gt;&gt;();
} else {
Seq&lt;T&gt;.Zipper&lt;S&gt; tailZipper =
tail.new Zipper&lt;S&gt;();
return new Seq&lt;Pair&lt;T,S&gt;&gt;(
new Pair&lt;T,S&gt;(head, that.head),
tailZipper.zip(that.tail));
}
}
}
}
class Pair&lt;T, S&gt; {
T fst; S snd;
Pair(T f, S s) { fst = f; snd = s; }
}
class Test {
public static void main(String[] args) {
Seq&lt;String&gt; strs =
new Seq&lt;String&gt;(
"a",
new Seq&lt;String&gt;("b",
new Seq&lt;String&gt;()));
Seq&lt;Number&gt; nums =
new Seq&lt;Number&gt;(
new Integer(1),
new Seq&lt;Number&gt;(new Double(1.5),
new Seq&lt;Number&gt;()));
Seq&lt;String&gt;.Zipper&lt;Number&gt; zipper =
strs.new Zipper&lt;Number&gt;();
Seq&lt;Pair&lt;String,Number&gt;&gt; combined =
zipper.zip(nums);
}
}
</pre></div>
</div><br class="example-break"></div>
<div class="section" title="8.1.3.&nbsp;Inner Classes and Enclosing Instances">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.3"></a>8.1.3.&nbsp;Inner Classes and Enclosing Instances
</h3>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.1.3-100"></a>An <span class="emphasis"><em>inner
class</em></span> is a nested class that is not explicitly or
implicitly declared <code class="literal">static</code>.
</p>
<p class="norm"><a name="jls-8.1.3-110"></a>An inner class may be a
non-<code class="literal">static</code> member class (<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>), a local class
(<a class="xref" href="jls-14.html#jls-14.3" title="14.3.&nbsp;Local Class Declarations">&sect;14.3</a>), or an anonymous class
(<a class="xref" href="jls-15.html#jls-15.9.5" title="15.9.5.&nbsp;Anonymous Class Declarations">&sect;15.9.5</a>). A member class of an interface is
implicitly <code class="literal">static</code> (<a class="xref" href="jls-9.html#jls-9.5" title="9.5.&nbsp;Member Type Declarations">&sect;9.5</a>) so is never considered
to be an inner class.
</p>
<p class="norm-error"><a name="jls-8.1.3-120"></a>It is a
compile-time error if an inner class declares a static initializer
(<a class="xref" href="jls-8.html#jls-8.7" title="8.7.&nbsp;Static Initializers">&sect;8.7</a>).
</p>
<p class="norm-error"><a name="jls-8.1.3-130"></a>It is a
compile-time error if an inner class declares a member that is
explicitly or implicitly <code class="literal">static</code>, unless the member is
a constant variable
(<a class="xref" href="jls-4.html#jls-4.12.4" title="4.12.4.&nbsp;final Variables">&sect;4.12.4</a>).
</p>
<p class="norm-static"><a name="jls-8.1.3-140"></a>An inner
class may inherit <code class="literal">static</code> members that are
not constant variables even though it
cannot declare them.
</p>
<p class="norm-static"><a name="jls-8.1.3-150"></a>A nested
class that is not an inner class may declare <code class="literal">static</code> members freely,
in accordance with the usual rules of the Java programming language.
</p>
<div class="example"><a name="d5e11104"></a><p class="title"><b>Example&nbsp;8.1.3-1.&nbsp;Inner Class Declarations and Static Members</b></p>
<div class="example-contents"><pre class="programlisting">
class HasStatic {
static int j = 100;
}
class Outer {
class Inner extends HasStatic {
static final int x = 3; // OK: constant variable
static int y = 4; // Compile-time error: an inner class
}
static class NestedButNotInner{
static int z = 5; // OK: not an inner class
}
interface NeverInner {} // Interfaces are never inner
}
</pre></div>
</div><br class="example-break"><p class="norm"><a name="jls-8.1.3-200"></a>A statement or
expression <span class="emphasis"><em>occurs in a static context</em></span> if and only
if the innermost method, constructor, instance initializer, static
initializer, field initializer, or explicit constructor invocation
statement enclosing the statement or expression is a static method, a
static initializer, the variable initializer of a static variable, or
an explicit constructor invocation statement
(<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>
<p class="norm-static"><a name="jls-8.1.3-300"></a>An inner class
<span class="type">C</span> is a <span class="emphasis"><em>direct inner class of a class or interface
<span class="type">O</span></em></span> if <span class="type">O</span> is the immediately enclosing type declaration of
<span class="type">C</span> and the declaration of <span class="type">C</span> does not occur in a static
context.
</p>
<p class="norm-static"><a name="jls-8.1.3-310"></a>A class <span class="type">C</span> is
an <span class="emphasis"><em>inner class of class or interface <span class="type">O</span></em></span> if it is
either a direct inner class of <span class="type">O</span> or an inner class of an inner class
of <span class="type">O</span>.
</p>
<p class="note">It is unusual, but possible, for the immediately
enclosing type declaration of an inner class to be an interface. This
only occurs if the class is declared in a default method body
(<a class="xref" href="jls-9.html#jls-9.4" title="9.4.&nbsp;Method Declarations">&sect;9.4</a>). Specifically, it occurs if an anonymous
or local class is declared in a default method body, or a member class
is declared in the body of an anonymous class that is declared in a
default method body.
</p>
<p class="norm-static"><a name="jls-8.1.3-320"></a>A class or
interface <span class="type">O</span> is the <span class="emphasis"><em>zeroth lexically enclosing type
declaration of itself</em></span>.
</p>
<p class="norm-static"><a name="jls-8.1.3-330"></a>A class <span class="type">O</span> is
the <span class="emphasis"><em>n'th lexically enclosing type declaration of a class
<span class="type">C</span></em></span> if it is the immediately enclosing type declaration of
the <span class="emphasis"><em>n-1</em></span>'th lexically enclosing type declaration
of <span class="type">C</span>.
</p>
<p class="norm-static"><a name="jls-8.1.3-400"></a>An instance <code class="varname">i</code>
of a direct inner class <span class="type">C</span> of a class or interface <span class="type">O</span> is associated
with an instance of <span class="type">O</span>, known as the <span class="emphasis"><em>immediately enclosing
instance of <code class="varname">i</code></em></span>. The immediately enclosing instance of an
object, if any, is determined when the object is created
(<a class="xref" href="jls-15.html#jls-15.9.2" title="15.9.2.&nbsp;Determining Enclosing Instances">&sect;15.9.2</a>).
</p>
<p class="norm-static"><a name="jls-8.1.3-410"></a>An object
<code class="varname">o</code> is the <span class="emphasis"><em>zeroth lexically enclosing instance of
itself</em></span>.
</p>
<p class="norm-static"><a name="jls-8.1.3-420"></a>An object
<code class="varname">o</code> is the <span class="emphasis"><em>n'th lexically enclosing instance of an instance
<code class="varname">i</code></em></span> if it is the immediately enclosing instance of
the <span class="emphasis"><em>n-1</em></span>'th lexically enclosing instance of
<code class="varname">i</code>.
</p>
<p class="norm-static"><a name="jls-8.1.3-430"></a>An
instance of an inner class <span class="type">I</span> whose declaration occurs in a static
context has no lexically enclosing instances. However, if <span class="type">I</span> is
immediately declared within a static method or static initializer then
<span class="type">I</span> does have an <span class="emphasis"><em>enclosing block</em></span>, which is the
innermost block statement lexically enclosing the declaration of
<span class="type">I</span>.
</p>
<p class="norm-static"><a name="jls-8.1.3-440"></a>For every
superclass <span class="type">S</span> of <span class="type">C</span> which is itself a direct inner class of a class
or interface <span class="type">SO</span>, there is an instance of <span class="type">SO</span>
associated with <code class="varname">i</code>, known as the <span class="emphasis"><em>immediately enclosing
instance of <code class="varname">i</code> with respect to <span class="type">S</span></em></span>. The immediately
enclosing instance of an object with respect to its class' direct
superclass, if any, is determined when the superclass constructor is
invoked via an explicit constructor invocation statement
(<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>
<p class="norm-dynamic"><a name="jls-8.1.3-500"></a>When an
inner class (whose declaration does not occur in a static context)
refers to an instance variable that is a member of a lexically
enclosing type declaration, the variable of the corresponding
lexically enclosing instance is used.
</p>
<p class="norm-error"><a name="jls-8.1.3-510"></a>Any local
variable, formal parameter, or exception parameter used but not
declared in an inner class must either be declared <code class="literal">final</code> or be
effectively final (<a class="xref" href="jls-4.html#jls-4.12.4" title="4.12.4.&nbsp;final Variables">&sect;4.12.4</a>), or a compile-time
error occurs where the use is attempted.
</p>
<p class="norm-error"><a name="jls-8.1.3-520"></a>Any local
variable used but not declared in an inner class must be definitely
assigned (<a class="xref" href="jls-16.html" title="Chapter&nbsp;16.&nbsp;Definite Assignment">&sect;16 (<i>Definite Assignment</i>)</a>) before the body of the inner
class, or a compile-time error occurs.
</p>
<p class="note">Similar rules on variable use apply in the body of a
lambda expression (<a class="xref" href="jls-15.html#jls-15.27.2" title="15.27.2.&nbsp;Lambda Body">&sect;15.27.2</a>).
</p>
<p class="norm-error"><a name="jls-8.1.3-530"></a>A blank
<code class="literal">final</code> field (<a class="xref" href="jls-4.html#jls-4.12.4" title="4.12.4.&nbsp;final Variables">&sect;4.12.4</a>) of a lexically enclosing
type declaration may not be assigned within an inner class, or a
compile-time error occurs.
</p>
<div class="example"><a name="d5e11178"></a><p class="title"><b>Example&nbsp;8.1.3-2.&nbsp;Inner Class Declarations</b></p>
<div class="example-contents"><pre class="programlisting">
class Outer {
int i = 100;
static void classMethod() {
final int l = 200;
class LocalInStaticContext {
int k = i; // Compile-time error
int m = l; // OK
}
}
void foo() {
class Local { // A local class
int j = i;
}
}
}
</pre><p class="note">The declaration of
class <code class="literal">LocalInStaticContext</code> occurs in a static
context due to being within the static
method <code class="literal">classMethod</code>. Instance variables of
class <code class="literal">Outer</code> are not available within the body of a
static method. In particular, instance variables
of <code class="literal">Outer</code> are not available inside the body
of <code class="literal">LocalInStaticContext</code>. However, local variables
from the surrounding method may be referred to without error (provided
they are marked <code class="literal">final</code>).
</p>
<p class="note">Inner classes whose declarations do not occur in a
static context may freely refer to the instance variables of their
enclosing type declaration. An instance variable is always defined
with respect to an instance. In the case of instance variables of an
enclosing type declaration, the instance variable must be defined with
respect to an enclosing instance of that declared type. For example,
the class <code class="literal">Local</code> above has an enclosing instance of
class <code class="literal">Outer</code>. As a further example:
</p><pre class="programlisting">
class WithDeepNesting {
boolean toBe;
WithDeepNesting(boolean b) { toBe = b; }
class Nested {
boolean theQuestion;
class DeeplyNested {
DeeplyNested(){
theQuestion = toBe || !toBe;
}
}
}
}
</pre><p class="note">Here, every instance
of <code class="literal">WithDeepNesting.Nested.DeeplyNested</code> has an
enclosing instance of class <code class="literal">WithDeepNesting.Nested</code>
(its immediately enclosing instance) and an enclosing instance of
class <code class="literal">WithDeepNesting</code> (its 2nd lexically enclosing
instance).
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="8.1.4.&nbsp;Superclasses and Subclasses">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.4"></a>8.1.4.&nbsp;Superclasses and Subclasses
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1.4-100"></a>The
optional <code class="literal">extends</code> clause in a normal class declaration specifies
the <span class="emphasis"><em>direct superclass</em></span> of the current
class.
</p>
<div id="jls-8.1.4-110" class="productionset"><a name="jls-8.1.4-110"></a>
<div class="production"><a name="jls-Superclass"></a>
<div class="lhs">Superclass:</div>
<div class="rhs">
<code class="literal">extends</code> <a href="jls-4.html#jls-ClassType" title="ClassType">ClassType</a>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-8.1.4-200"></a>The
<code class="literal">extends</code> clause must not appear in the definition of the class
<code class="literal">Object</code>, or a compile-time error occurs, because it is the primordial
class and has no direct superclass.
</p>
<p class="norm-error"><a name="jls-8.1.4-210"></a>The <span class="emphasis"><em>ClassType</em></span> must name an
accessible class type (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>), or a compile-time
error occurs.
</p>
<p class="norm-error"><a name="jls-8.1.4-220"></a>It is a
compile-time error if the <span class="emphasis"><em>ClassType</em></span> names a class
that is <code class="literal">final</code>, because <code class="literal">final</code> classes are not allowed to have
subclasses (<a class="xref" href="jls-8.html#jls-8.1.1.2" title="8.1.1.2.&nbsp;final Classes">&sect;8.1.1.2</a>).
</p>
<p class="norm-error"><a name="jls-8.1.4-230"></a>It is a
compile-time error if the <span class="emphasis"><em>ClassType</em></span> names the
class <code class="literal">Enum</code> or any invocation of <code class="literal">Enum</code>
(<a class="xref" href="jls-8.html#jls-8.9" title="8.9.&nbsp;Enum Types">&sect;8.9</a>).
</p>
<p class="norm-error"><a name="jls-8.1.4-240"></a>If
the <span class="emphasis"><em>ClassType</em></span> has type arguments, it must denote
a well-formed parameterized type (<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>), and none
of the type arguments may be wildcard type arguments, or a
compile-time error occurs.
</p>
<p class="norm-static"><a name="jls-8.1.4-300"></a>Given a
(possibly generic) class declaration <span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code>
(<span class="emphasis"><em>n</em></span> <span class="symbol">&#8805;</span> 0, <span class="type">C</span> <span class="symbol">&#8800;</span> <code class="literal">Object</code>), the <span class="emphasis"><em>direct
superclass</em></span> of the class type
<span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> is the type given in the <code class="literal">extends</code>
clause of the declaration of <span class="type">C</span> if an <code class="literal">extends</code> clause is present, or
<code class="literal">Object</code> otherwise.
</p>
<p class="norm-static"><a name="jls-8.1.4-310"></a>Given a
generic class declaration <span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> (<span class="emphasis"><em>n</em></span>
&gt; 0), the <span class="emphasis"><em>direct superclass</em></span> of the
parameterized class type <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code>, where <span class="type">T<sub>i</sub></span>
(1 <span class="symbol">&#8804;</span> <span class="emphasis"><em>i</em></span> <span class="symbol">&#8804;</span> <span class="emphasis"><em>n</em></span>) is a type, is <span class="type">D</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>
<span class="symbol">&#952;</span>,...,<span class="type">U<sub>k</sub></span> <span class="symbol">&#952;</span><code class="literal">&gt;</code>, where
<span class="type">D</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>,...,<span class="type">U<sub>k</sub></span><code class="literal">&gt;</code> is the direct superclass of
<span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> and <span class="symbol">&#952;</span> is the
substitution <code class="literal">[<span class="type">F<sub>1</sub></span>:=<span class="type">T<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span>:=<span class="type">T<sub>n</sub></span>]</code>.
</p>
<p class="norm"><a name="jls-8.1.4-330"></a>A class is said to be
a <span class="emphasis"><em>direct subclass</em></span> of its direct superclass. The
direct superclass is the class from whose implementation the
implementation of the current class is derived.
</p>
<p class="norm"><a name="jls-8.1.4-400"></a>The
<span class="emphasis"><em>subclass</em></span> relationship is the transitive closure
of the direct subclass relationship. A class <span class="type">A</span> is a subclass of
class <span class="type">C</span> if either of the following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-8.1.4-400-A"></a><span class="type">A</span> is the direct
subclass of <span class="type">C</span></p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.4-400-B"></a>There exists a class
<span class="type">B</span> such that <span class="type">A</span> is a subclass of <span class="type">B</span>, and <span class="type">B</span> is a subclass of
<span class="type">C</span>, applying this definition recursively.
</p>
</li>
</ul>
</div>
<p class="norm"><a name="jls-8.1.4-410"></a>Class <span class="type">C</span> is said to be
a <span class="emphasis"><em>superclass</em></span> of class <span class="type">A</span> whenever <span class="type">A</span> is a
subclass of <span class="type">C</span>.
</p>
<div class="example"><a name="d5e11331"></a><p class="title"><b>Example&nbsp;8.1.4-1.&nbsp;Direct Superclasses and Subclasses</b></p>
<div class="example-contents"><pre class="programlisting">
class Point { int x, y; }
final class ColoredPoint extends Point { int color; }
class Colored3DPoint extends ColoredPoint { int z; } // error
</pre><p class="note">Here, the relationships are as follows:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">The class <code class="literal">Point</code> is a direct
subclass of <code class="literal">Object</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">Object</code> is the direct superclass of
the class <code class="literal">Point</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">ColoredPoint</code> is a
direct subclass of class <code class="literal">Point</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">Point</code> is the direct
superclass of
class <code class="literal">ColoredPoint</code>.
</p>
</li>
</ul>
</div>
<p class="note">The declaration of
class <code class="literal">Colored3dPoint</code> causes a compile-time error
because it attempts to extend the final
class <code class="literal">ColoredPoint</code>.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e11355"></a><p class="title"><b>Example&nbsp;8.1.4-2.&nbsp;Superclasses and Subclasses</b></p>
<div class="example-contents"><pre class="programlisting">
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
final class Colored3dPoint extends ColoredPoint { int z; }
</pre><p class="note">Here, the relationships are as follows:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">The class <code class="literal">Point</code> is a
superclass of class <code class="literal">ColoredPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">Point</code> is a
superclass of class <code class="literal">Colored3dPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">ColoredPoint</code> is a
subclass of class <code class="literal">Point</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">ColoredPoint</code> is a
superclass of class <code class="literal">Colored3dPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">Colored3dPoint</code> is a
subclass of class <code class="literal">ColoredPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The class <code class="literal">Colored3dPoint</code> is a
subclass of class <code class="literal">Point</code>.
</p>
</li>
</ul>
</div>
</div>
</div><br class="example-break"><p class="norm"><a name="jls-8.1.4-500"></a>A class
<span class="type">C</span> <span class="emphasis"><em>directly depends</em></span> on a type <span class="type">T</span> if <span class="type">T</span> is
mentioned in the <code class="literal">extends</code> or <code class="literal">implements</code> clause of <span class="type">C</span> either as a
superclass or superinterface, or as a qualifier of a superclass or
superinterface name.
</p>
<p class="norm"><a name="jls-8.1.4-510"></a>A class <span class="type">C</span>
<span class="emphasis"><em>depends</em></span> on a reference type <span class="type">T</span> if any of the
following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-8.1.4-510-A"></a>
<span class="type">C</span> directly depends on <span class="type">T</span>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.4-510-B"></a>
<span class="type">C</span> directly depends on an interface <span class="type">I</span> that depends
(<a class="xref" href="jls-9.html#jls-9.1.3" title="9.1.3.&nbsp;Superinterfaces and Subinterfaces">&sect;9.1.3</a>) on <span class="type">T</span>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.4-510-C"></a>
<span class="type">C</span> directly depends on a class <span class="type">D</span> that depends on
<span class="type">T</span> (using this definition recursively).
</p>
</li>
</ul>
</div>
<p class="norm-error"><a name="jls-8.1.4-520"></a>It is a
compile-time error if a class depends on itself.
</p>
<p class="norm-dynamic"><a name="jls-8.1.4-530"></a>If
circularly declared classes are detected at run time, as classes are
loaded, then a <code class="literal">ClassCircularityError</code> is thrown
(<a class="xref" href="jls-12.html#jls-12.2.1" title="12.2.1.&nbsp;The Loading Process">&sect;12.2.1</a>).
</p>
<div class="example"><a name="d5e11416"></a><p class="title"><b>Example&nbsp;8.1.4-3.&nbsp;Class Depends on Itself</b></p>
<div class="example-contents"><pre class="programlisting">
class Point extends ColoredPoint { int x, y; }
class ColoredPoint extends Point { int color; }
</pre><p class="note">This program causes a compile-time error because
class <code class="literal">Point</code> depends on itself.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="8.1.5.&nbsp;Superinterfaces">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.5"></a>8.1.5.&nbsp;Superinterfaces
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1.5-100"></a>The
optional <code class="literal">implements</code> clause in a class declaration lists the names of
interfaces that are direct superinterfaces of the class being
declared.
</p>
<div id="jls-8.1.5-110" class="productionset"><a name="jls-8.1.5-110"></a>
<div class="production"><a name="jls-Superinterfaces"></a>
<div class="lhs">Superinterfaces:</div>
<div class="rhs">
<code class="literal">implements</code> <a href="jls-8.html#jls-InterfaceTypeList" title="InterfaceTypeList">InterfaceTypeList</a>
</div>
</div>
<div class="production"><a name="jls-InterfaceTypeList"></a>
<div class="lhs">InterfaceTypeList:</div>
<div class="rhs">
<a href="jls-4.html#jls-InterfaceType" title="InterfaceType">InterfaceType</a> {<code class="literal">,</code> <a href="jls-4.html#jls-InterfaceType" title="InterfaceType">InterfaceType</a>}
</div>
</div>
</div>
<p class="norm-error"><a name="jls-8.1.5-200"></a>Each <span class="emphasis"><em>InterfaceType</em></span> must
name an accessible interface type (<a class="xref" href="jls-6.html#jls-6.6" title="6.6.&nbsp;Access Control">&sect;6.6</a>), or a
compile-time error occurs.
</p>
<p class="norm-error"><a name="jls-8.1.5-210"></a>If
an <span class="emphasis"><em>InterfaceType</em></span> has type arguments, it must
denote a well-formed parameterized type (<a class="xref" href="jls-4.html#jls-4.5" title="4.5.&nbsp;Parameterized Types">&sect;4.5</a>),
and none of the type arguments may be wildcard type arguments, or a
compile-time error occurs.
</p>
<p class="norm-error"><a name="jls-8.1.5-220"></a>It is a
compile-time error if the same interface is mentioned as a direct
superinterface more than once in a single <code class="literal">implements</code> clause. This is
true even if the interface is named in different ways.
</p>
<div class="example"><a name="d5e11445"></a><p class="title"><b>Example&nbsp;8.1.5-1.&nbsp;Illegal Superinterfaces</b></p>
<div class="example-contents"><pre class="programlisting">
class Redundant implements java.lang.Cloneable, Cloneable {
int x;
}
</pre><p class="note">This program results in a compile-time error because
the names <code class="literal">java.lang.Cloneable</code> and <code class="literal">Cloneable</code> refer
to the same interface.
</p>
</div>
</div><br class="example-break"><p class="norm-static"><a name="jls-8.1.5-300"></a>Given a
(possibly generic) class declaration <span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code>
(<span class="emphasis"><em>n</em></span> <span class="symbol">&#8805;</span> 0, <span class="type">C</span> <span class="symbol">&#8800;</span> <code class="literal">Object</code>), the <span class="emphasis"><em>direct
superinterfaces</em></span> of the class type
<span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> are the types given in the
<code class="literal">implements</code> clause of the declaration of <span class="type">C</span>, if an <code class="literal">implements</code>
clause is present.
</p>
<p class="norm-static"><a name="jls-8.1.5-310"></a>Given a
generic class declaration <span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> (<span class="emphasis"><em>n</em></span>
&gt; 0), the <span class="emphasis"><em>direct superinterfaces</em></span> of the
parameterized class type <span class="type">C</span><code class="literal">&lt;</code><span class="type">T<sub>1</sub></span>,...,<span class="type">T<sub>n</sub></span><code class="literal">&gt;</code>, where <span class="type">T<sub>i</sub></span>
(1 <span class="symbol">&#8804;</span> <span class="emphasis"><em>i</em></span> <span class="symbol">&#8804;</span> <span class="emphasis"><em>n</em></span>) is a type, are all types <span class="type">I</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>
<span class="symbol">&#952;</span>,...,<span class="type">U<sub>k</sub></span> <span class="symbol">&#952;</span><code class="literal">&gt;</code>, where
<span class="type">I</span><code class="literal">&lt;</code><span class="type">U<sub>1</sub></span>,...,<span class="type">U<sub>k</sub></span><code class="literal">&gt;</code> is a direct superinterface of
<span class="type">C</span><code class="literal">&lt;</code><span class="type">F<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span><code class="literal">&gt;</code> and <span class="symbol">&#952;</span> is the
substitution <code class="literal">[<span class="type">F<sub>1</sub></span>:=<span class="type">T<sub>1</sub></span>,...,<span class="type">F<sub>n</sub></span>:=<span class="type">T<sub>n</sub></span>]</code>.
</p>
<p class="norm-static"><a name="jls-8.1.5-400"></a>An
interface type <span class="type">I</span> is a <span class="emphasis"><em>superinterface</em></span> of class
type <span class="type">C</span> if any of the following is true:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-8.1.5-400-A"></a><span class="type">I</span> is a direct
superinterface of <span class="type">C</span>.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.1.5-400-B"></a><span class="type">C</span> has some direct
superinterface <span class="type">J</span> for which <span class="type">I</span> is a superinterface,
using the definition of "superinterface of an interface" given
in <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"><a name="jls-8.1.5-400-C"></a><span class="type">I</span> is a superinterface
of the direct superclass of <span class="type">C</span>.
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-8.1.5-410"></a>A class
can have a superinterface in more than one way.
</p>
<p class="norm"><a name="jls-8.1.5-420"></a>A class is said
to <span class="emphasis"><em>implement</em></span> all its superinterfaces.
</p>
<p class="norm-error"><a name="jls-8.1.5-430"></a>A class may
not at the same time be a subtype of two interface types which are
different parameterizations of the same generic interface
(<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>), or a subtype of a parameterization of a
generic interface and a raw type naming that same generic interface,
or a compile-time error occurs.
</p>
<p class="note">This requirement was introduced in order to support
translation by type erasure (<a class="xref" href="jls-4.html#jls-4.6" title="4.6.&nbsp;Type Erasure">&sect;4.6</a>).
</p>
<div class="example"><a name="d5e11557"></a><p class="title"><b>Example&nbsp;8.1.5-2.&nbsp;Superinterfaces</b></p>
<div class="example-contents"><pre class="programlisting">
interface Colorable {
void setColor(int color);
int getColor();
}
enum Finish { MATTE, GLOSSY }
interface Paintable extends Colorable {
void setFinish(Finish finish);
Finish getFinish();
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
public int getColor() { return color; }
}
class PaintedPoint extends ColoredPoint implements Paintable {
Finish finish;
public void setFinish(Finish finish) {
this.finish = finish;
}
public Finish getFinish() { return finish; }
}
</pre><p class="note">Here, the relationships are as follows:</p>
<div class="note">
<ul class="note" type="disc">
<li class="listitem">
<p class="note">The interface <code class="literal">Paintable</code> is a
superinterface of class <code class="literal">PaintedPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The interface <code class="literal">Colorable</code> is a
superinterface of class <code class="literal">ColoredPoint</code> and of
class <code class="literal">PaintedPoint</code>.
</p>
</li>
<li class="listitem">
<p class="note">The interface <code class="literal">Paintable</code> is a
subinterface of the interface <code class="literal">Colorable</code>,
and <code class="literal">Colorable</code> is a superinterface
of <code class="literal">Paintable</code>, as defined in
<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>
</ul>
</div>
<p class="note">The class <code class="literal">PaintedPoint</code>
has <code class="literal">Colorable</code> as a superinterface both because it
is a superinterface of <code class="literal">ColoredPoint</code> and because it
is a superinterface of <code class="literal">Paintable</code>.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e11583"></a><p class="title"><b>Example&nbsp;8.1.5-3.&nbsp;Illegal Multiple Inheritance of an Interface</b></p>
<div class="example-contents"><pre class="programlisting">
interface I&lt;T&gt; {}
class B implements I&lt;Integer&gt; {}
class C extends B implements I&lt;String&gt; {}
</pre><p class="note">Class <code class="literal">C</code> causes a compile-time
error because it attempts to be a subtype of both <span class="type">I</span>&lt;<code class="literal">Integer</code>&gt;
and <span class="type">I</span>&lt;<code class="literal">String</code>&gt;.
</p>
</div>
</div><br class="example-break"><p class="norm-static"><a name="jls-8.1.5-500"></a>Unless the class
being declared is <code class="literal">abstract</code>, all the <code class="literal">abstract</code> member methods of
each direct superinterface must be implemented
(<a class="xref" href="jls-8.html#jls-8.4.8.1" title="8.4.8.1.&nbsp;Overriding (by Instance Methods)">&sect;8.4.8.1</a>) either by a declaration in this class
or by an existing method declaration inherited from the direct
superclass or a direct superinterface, because a class that is not
<code class="literal">abstract</code> is not permitted to have <code class="literal">abstract</code> methods
(<a class="xref" href="jls-8.html#jls-8.1.1.1" title="8.1.1.1.&nbsp;abstract Classes">&sect;8.1.1.1</a>).
</p>
<p class="norm-static"><a name="jls-8.1.5-510"></a>Each default
method (<a class="xref" href="jls-9.html#jls-9.4.3" title="9.4.3.&nbsp;Interface Method Body">&sect;9.4.3</a>) of a superinterface of the class
may optionally be overridden by a method in the class; if not, the
default method is typically inherited and its behavior is as specified
by its default body.
</p>
<p class="norm-static"><a name="jls-8.1.5-520"></a>It is
permitted for a single method declaration in a class to implement
methods of more than one superinterface.
</p>
<div class="example"><a name="d5e11602"></a><p class="title"><b>Example&nbsp;8.1.5-3.&nbsp;Implementing Methods of a Superinterface</b></p>
<div class="example-contents"><pre class="programlisting">
interface Colorable {
void setColor(int color);
int getColor();
}
class Point { int x, y; };
class ColoredPoint extends Point implements Colorable {
int color;
}
</pre><p class="note">This program causes a compile-time error,
because <code class="literal">ColoredPoint</code> is not an <code class="literal">abstract</code> class but
fails to provide an implementation of
methods <code class="literal">setColor</code> and <code class="literal">getColor</code> of
the interface <code class="literal">Colorable</code>.
</p>
<p class="note">In the following program:</p><pre class="programlisting">
interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {
// You can tune a piano, but can you tuna fish?
public int getNumberOfScales() { return 91; }
}
</pre><p class="note">the method <code class="literal">getNumberOfScales</code> in
class <code class="literal">Tuna</code> has a name, signature, and return type
that matches the method declared in interface <code class="literal">Fish</code>
and also matches the method declared in
interface <code class="literal">Piano</code>; it is considered to implement
both.
</p>
<p class="note">On the other hand, in a situation such as
this:
</p><pre class="programlisting">
interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
// This declaration cannot be correct,
// no matter what type is used.
public ?? getNumberOfScales() { return 91; }
}
</pre><p class="note">it is impossible to declare a method
named <code class="literal">getNumberOfScales</code> whose signature and return
type are compatible with those of both the methods declared in
interface <code class="literal">Fish</code> and in
interface <code class="literal">StringBass</code>, because a class cannot have
multiple methods with the same signature and different primitive
return types (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>). Therefore, it is impossible
for a single class to implement both interface <code class="literal">Fish</code>
and interface <code class="literal">StringBass</code>
(<a class="xref" href="jls-8.html#jls-8.4.8" title="8.4.8.&nbsp;Inheritance, Overriding, and Hiding">&sect;8.4.8</a>).
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="8.1.6.&nbsp;Class Body and Member Declarations">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.1.6"></a>8.1.6.&nbsp;Class Body and Member Declarations
</h3>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1.6-100"></a>A <span class="emphasis"><em>class body</em></span> may contain
declarations of members of the class, that is, fields
(<a class="xref" href="jls-8.html#jls-8.3" title="8.3.&nbsp;Field Declarations">&sect;8.3</a>), methods (<a class="xref" href="jls-8.html#jls-8.4" title="8.4.&nbsp;Method Declarations">&sect;8.4</a>),
classes (<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>), and interfaces
(<a class="xref" href="jls-8.html#jls-8.5" title="8.5.&nbsp;Member Type Declarations">&sect;8.5</a>).
</p>
<p class="norm-static"><a name="jls-8.1.6-110"></a>A class
body may also contain instance initializers
(<a class="xref" href="jls-8.html#jls-8.6" title="8.6.&nbsp;Instance Initializers">&sect;8.6</a>), static initializers
(<a class="xref" href="jls-8.html#jls-8.7" title="8.7.&nbsp;Static Initializers">&sect;8.7</a>), and declarations of constructors
(<a class="xref" href="jls-8.html#jls-8.8" title="8.8.&nbsp;Constructor Declarations">&sect;8.8</a>) for the class.
</p>
<div id="jls-8.1.6-120" class="productionset"><a name="jls-8.1.6-120"></a>
<div class="production"><a name="jls-ClassBody"></a>
<div class="lhs">ClassBody:</div>
<div class="rhs">
<code class="literal">{</code> {<a href="jls-8.html#jls-ClassBodyDeclaration" title="ClassBodyDeclaration">ClassBodyDeclaration</a>} <code class="literal">}</code>
</div>
</div>
<div class="production"><a name="jls-ClassBodyDeclaration"></a>
<div class="lhs">ClassBodyDeclaration:</div>
<div class="rhs">
<a href="jls-8.html#jls-ClassMemberDeclaration" title="ClassMemberDeclaration">ClassMemberDeclaration</a> <br>
<a href="jls-8.html#jls-InstanceInitializer" title="InstanceInitializer">InstanceInitializer</a> <br>
<a href="jls-8.html#jls-StaticInitializer" title="StaticInitializer">StaticInitializer</a> <br>
<a href="jls-8.html#jls-ConstructorDeclaration" title="ConstructorDeclaration">ConstructorDeclaration</a>
</div>
</div>
<div class="production"><a name="jls-ClassMemberDeclaration"></a>
<div class="lhs">ClassMemberDeclaration:</div>
<div class="rhs">
<a href="jls-8.html#jls-FieldDeclaration" title="FieldDeclaration">FieldDeclaration</a> <br>
<a href="jls-8.html#jls-MethodDeclaration" title="MethodDeclaration">MethodDeclaration</a> <br>
<a href="jls-8.html#jls-ClassDeclaration" title="ClassDeclaration">ClassDeclaration</a> <br>
<a href="jls-9.html#jls-InterfaceDeclaration" title="InterfaceDeclaration">InterfaceDeclaration</a> <br>
<code class="literal">;</code>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.1.6-200"></a>The scope
and shadowing of a declaration of a member <code class="varname">m</code>
declared in or inherited by a class type <span class="type">C</span> is specified in
<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a> and <a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<p class="note">If <span class="type">C</span> itself is a nested class, there may be
definitions of the same kind (variable, method, or type) and name
as <code class="varname">m</code> in enclosing scopes. (The scopes may be
blocks, classes, or packages.) In all such cases, the
member <code class="varname">m</code> declared in or inherited by <span class="type">C</span> shadows
(<a class="xref" href="jls-6.html#jls-6.4.1" title="6.4.1.&nbsp;Shadowing">&sect;6.4.1</a>) the other definitions of the same kind
and name.
</p>
</div>
</div>
<div class="section" title="8.2.&nbsp;Class Members">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-8.2"></a>8.2.&nbsp;Class Members
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.2-100"></a>The members
of a class type are all of the following:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm-static"><a name="jls-8.2-100-A"></a>
Members inherited from its direct superclass
(<a class="xref" href="jls-8.html#jls-8.1.4" title="8.1.4.&nbsp;Superclasses and Subclasses">&sect;8.1.4</a>), except in class <code class="literal">Object</code>, which
has no direct superclass
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-8.2-100-B"></a>
Members inherited from any direct superinterfaces
(<a class="xref" href="jls-8.html#jls-8.1.5" title="8.1.5.&nbsp;Superinterfaces">&sect;8.1.5</a>)
</p>
</li>
<li class="listitem">
<p class="norm-static"><a name="jls-8.2-100-C"></a>
Members declared in the body of the class
(<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>)
</p>
</li>
</ul>
</div>
<p class="norm-static"><a name="jls-8.2-110"></a>Members of a
class that are declared <code class="literal">private</code> are not inherited by subclasses of
that class.
</p>
<p class="norm-static"><a name="jls-8.2-120"></a>Only members
of a class that are declared <code class="literal">protected</code> or <code class="literal">public</code> are inherited by
subclasses declared in a package other than the one in which the class
is declared.
</p>
<p class="norm-static"><a name="jls-8.2-130"></a>
Constructors, static initializers, and instance initializers are not
members and therefore are not inherited.
</p>
<p class="norm"><a name="jls-8.2-200"></a>We use the phrase <span class="emphasis"><em>the
type of a member</em></span> to denote:
</p>
<div class="norm">
<ul class="norm" type="disc">
<li class="listitem">
<p class="norm"><a name="jls-8.2-200-A"></a>
For a field, its type.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.2-200-B"></a>
For a method, an ordered 4-tuple consisting of:
</p>
<div class="norm">
<ul class="norm" type="circle">
<li class="listitem">
<p class="norm-static"><a name="jls-8.2-200-B-A"></a>
type parameters: the declarations of any type parameters of
the method member.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.2-200-B-B"></a>
argument types: a list of the types of the arguments to the
method member.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.2-200-B-C"></a>
return type: the return type of the method member.
</p>
</li>
<li class="listitem">
<p class="norm"><a name="jls-8.2-200-B-D"></a>
<code class="literal">throws</code> clause: exception types declared in the <code class="literal">throws</code>
clause of the method member.
</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<p class="norm"><a name="jls-8.2-210"></a>Fields, methods, and member
types of a class type may have the same name, since they are used in
different contexts and are disambiguated by different lookup
procedures (<a class="xref" href="jls-6.html#jls-6.5" title="6.5.&nbsp;Determining the Meaning of a Name">&sect;6.5</a>). However, this is discouraged
as a matter of style.
</p>
<div class="example"><a name="d5e11713"></a><p class="title"><b>Example&nbsp;8.2-1.&nbsp;Use of Class Members</b></p>
<div class="example-contents"><pre class="programlisting">
class Point {
int x, y;
private Point() { reset(); }
Point(int x, int y) { this.x = x; this.y = y; }
private void reset() { this.x = 0; this.y = 0; }
}
class ColoredPoint extends Point {
int color;
void clear() { reset(); } // error
}
class Test {
public static void main(String[] args) {
ColoredPoint c = new ColoredPoint(0, 0); // error
c.reset(); // error
}
}
</pre><p class="note">This program causes four compile-time errors.</p>
<p class="note">One error occurs
because <code class="literal">ColoredPoint</code> has no constructor declared
with two <code class="literal">int</code> parameters, as requested by the use
in <code class="literal">main</code>. This illustrates the fact
that <code class="literal">ColoredPoint</code> does not inherit the constructors
of its superclass <code class="literal">Point</code>.
</p>
<p class="note">Another error occurs
because <code class="literal">ColoredPoint</code> declares no constructors, and
therefore a default constructor for it is implicitly declared
(<a class="xref" href="jls-8.html#jls-8.8.9" title="8.8.9.&nbsp;Default Constructor">&sect;8.8.9</a>), and this default constructor is
equivalent to:
</p><pre class="screen">
ColoredPoint() { super(); }
</pre><p class="note">which invokes the constructor, with no arguments,
for the direct superclass of the
class <code class="literal">ColoredPoint</code>. The error is that the
constructor for <code class="literal">Point</code> that takes no arguments is
<code class="literal">private</code>, and therefore is not accessible outside the
class <code class="literal">Point</code>, even through a superclass constructor
invocation (<a class="xref" href="jls-8.html#jls-8.8.7" title="8.8.7.&nbsp;Constructor Body">&sect;8.8.7</a>).
</p>
<p class="note">Two more errors occur because the
method <code class="literal">reset</code> of class <code class="literal">Point</code> is
<code class="literal">private</code>, and therefore is not inherited by
class <code class="literal">ColoredPoint</code>. The method invocations in
method <code class="literal">clear</code> of
class <code class="literal">ColoredPoint</code> and in
method <code class="literal">main</code> of class <code class="literal">Test</code> are
therefore not correct.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e11742"></a><p class="title"><b>Example&nbsp;8.2-2.&nbsp;Inheritance of Class Members with Package Access</b></p>
<div class="example-contents">
<p class="note">Consider the example where
the <code class="literal">points</code> package declares two compilation
units:
</p><pre class="programlisting">
package points;
public class Point {
int x, y;
public void move(int dx, int dy) { x += dx; y += dy; }
}
</pre><p class="note">and:</p><pre class="programlisting">
package points;
public class Point3d extends Point {
int z;
public void move(int dx, int dy, int dz) {
x += dx; y += dy; z += dz;
}
}
</pre><p class="note">and a third compilation unit, in another package,
is:
</p><pre class="programlisting">
import points.Point3d;
class Point4d extends Point3d {
int w;
public void move(int dx, int dy, int dz, int dw) {
x += dx; y += dy; z += dz; w += dw; // compile-time errors
}
}
</pre><p class="note">Here both classes in the <code class="literal">points</code>
package compile. The class <code class="literal">Point3d</code> inherits the
fields <code class="literal">x</code> and <code class="literal">y</code> of
class <code class="literal">Point</code>, because it is in the same package
as <code class="literal">Point</code>. The class <code class="literal">Point4d</code>,
which is in a different package, does not inherit the
fields <code class="literal">x</code> and <code class="literal">y</code> of
class <code class="literal">Point</code> or the field <code class="literal">z</code> of
class <code class="literal">Point3d</code>, and so fails to compile.
</p>
<p class="note">A better way to write the third compilation unit
would be:
</p><pre class="programlisting">
import points.Point3d;
class Point4d extends Point3d {
int w;
public void move(int dx, int dy, int dz, int dw) {
super.move(dx, dy, dz); w += dw;
}
}
</pre><p class="note">using the <code class="literal">move</code> method of the
superclass <code class="literal">Point3d</code> to
process <code class="literal">dx</code>, <code class="literal">dy</code>,
and <code class="literal">dz</code>. If <code class="literal">Point4d</code> is written in
this way, it will compile without errors.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e11773"></a><p class="title"><b>Example&nbsp;8.2-3.&nbsp;Inheritance of <code class="literal">public</code> and <code class="literal">protected</code> Class Members</b></p>
<div class="example-contents">
<p class="note">Given the class <code class="literal">Point</code>:
</p><pre class="programlisting">
package points;
public class Point {
public int x, y;
protected int useCount = 0;
static protected int totalUseCount = 0;
public void move(int dx, int dy) {
x += dx; y += dy; useCount++; totalUseCount++;
}
}
</pre><p class="note">the <code class="literal">public</code> and <code class="literal">protected</code>
fields <code class="literal">x</code>, <code class="literal">y</code>,
<code class="literal">useCount</code>, and <code class="literal">totalUseCount</code> are
inherited in all subclasses of <code class="literal">Point</code>.
</p>
<p class="note">Therefore, this test program, in another package,
can be compiled successfully:
</p><pre class="programlisting">
class Test extends points.Point {
public void moveBack(int dx, int dy) {
x -= dx; y -= dy; useCount++; totalUseCount++;
}
}
</pre></div>
</div><br class="example-break"><div class="example"><a name="d5e11790"></a><p class="title"><b>Example&nbsp;8.2-4.&nbsp;Inheritance of <code class="literal">private</code> Class Members</b></p>
<div class="example-contents"><pre class="programlisting">
class Point {
int x, y;
void move(int dx, int dy) {
x += dx; y += dy; totalMoves++;
}
private static int totalMoves;
void printMoves() { System.out.println(totalMoves); }
}
class Point3d extends Point {
int z;
void move(int dx, int dy, int dz) {
super.move(dx, dy); z += dz; totalMoves++; // error
}
}
</pre><p class="note">Here, the class
variable <code class="literal">totalMoves</code> can be used only within the
class <code class="literal">Point</code>; it is not inherited by the
subclass <code class="literal">Point3d</code>. A compile-time error occurs
because method move of class <code class="literal">Point3d</code> tries to
increment <code class="literal">totalMoves</code>.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e11800"></a><p class="title"><b>Example&nbsp;8.2-5.&nbsp;Accessing Members of Inaccessible Classes</b></p>
<div class="example-contents">
<p class="note">Even though a class might not be declared <code class="literal">public</code>,
instances of the class might be available at run time to code outside
the package in which it is declared by means of a <code class="literal">public</code> superclass
or superinterface. An instance of the class can be assigned to a
variable of such a <code class="literal">public</code> type. An invocation of a <code class="literal">public</code> method
of the object referred to by such a variable may invoke a method of
the class if it implements or overrides a method of the <code class="literal">public</code>
superclass or superinterface. (In this situation, the method is
necessarily declared <code class="literal">public</code>, even though it is declared in a class
that is not <code class="literal">public</code>.)
</p>
<p class="note">Consider the compilation unit:</p><pre class="programlisting">
package points;
public class Point {
public int x, y;
public void move(int dx, int dy) {
x += dx; y += dy;
}
}
</pre><p class="note">and another compilation unit of another
package:
</p><pre class="programlisting">
package morePoints;
class Point3d extends points.Point {
public int z;
public void move(int dx, int dy, int dz) {
super.move(dx, dy); z += dz;
}
public void move(int dx, int dy) {
move(dx, dy, 0);
}
}
public class OnePoint {
public static points.Point getOne() {
return new Point3d();
}
}
</pre><p class="note">An
invocation <code class="literal">morePoints.OnePoint.getOne()</code> in yet a
third package would return a <code class="literal">Point3d</code> that can be
used as a <code class="literal">Point</code>, even though the
type <code class="literal">Point3d</code> is not available outside the
package <code class="literal">morePoints</code>. The two-argument version of
method <code class="literal">move</code> could then be invoked for that object,
which is permissible because method <code class="literal">move</code>
of <code class="literal">Point3d</code> is <code class="literal">public</code> (as it must be, for any
method that overrides a <code class="literal">public</code> method must itself be <code class="literal">public</code>,
precisely so that situations such as this will work out
correctly). The fields <code class="literal">x</code> and <code class="literal">y</code>
of that object could also be accessed from such a third
package.
</p>
<p class="note">While the field <code class="literal">z</code> of
class <code class="literal">Point3d</code> is <code class="literal">public</code>, it is not possible to
access this field from code outside the
package <code class="literal">morePoints</code>, given only a reference to an
instance of class <code class="literal">Point3d</code> in a
variable <code class="literal">p</code> of type <code class="literal">Point</code>. This
is because the expression <code class="literal">p.z</code> is not correct,
as <code class="literal">p</code> has type <code class="literal">Point</code> and
class <code class="literal">Point</code> has no field
named <code class="literal">z</code>; also, the
expression <code class="literal">((Point3d)p).z</code> is not correct, because
the class type <code class="literal">Point3d</code> cannot be referred to
outside package <code class="literal">morePoints</code>.
</p>
<p class="note">The declaration of the field <code class="literal">z</code> as
<code class="literal">public</code> is not useless, however. If there were to be, in
package <code class="literal">morePoints</code>, a <code class="literal">public</code>
subclass <code class="literal">Point4d</code> of the
class <code class="literal">Point3d</code>:
</p><pre class="programlisting">
package morePoints;
public class Point4d extends Point3d {
public int w;
public void move(int dx, int dy, int dz, int dw) {
super.move(dx, dy, dz); w += dw;
}
}
</pre><p class="note">then class <code class="literal">Point4d</code> would inherit
the field <code class="literal">z</code>, which, being <code class="literal">public</code>, could then be
accessed by code in packages other than <code class="literal">morePoints</code>,
through variables and expressions of the <code class="literal">public</code>
type <code class="literal">Point4d</code>.
</p>
</div>
</div><br class="example-break"></div>
<div class="section" title="8.3.&nbsp;Field Declarations">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="jls-8.3"></a>8.3.&nbsp;Field Declarations
</h2>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.3-100"></a>The
variables of a class type are introduced by <span class="emphasis"><em>field
declarations</em></span>.
</p>
<div id="jls-8.3-110" class="productionset"><a name="jls-8.3-110"></a>
<div class="production"><a name="jls-FieldDeclaration"></a>
<div class="lhs">FieldDeclaration:</div>
<div class="rhs">
{<a href="jls-8.html#jls-FieldModifier" title="FieldModifier">FieldModifier</a>}
<a href="jls-8.html#jls-UnannType" title="UnannType">UnannType</a>
<a href="jls-8.html#jls-VariableDeclaratorList" title="VariableDeclaratorList">VariableDeclaratorList</a> <code class="literal">;</code>
</div>
</div>
<div class="production"><a name="jls-VariableDeclaratorList"></a>
<div class="lhs">VariableDeclaratorList:</div>
<div class="rhs">
<a href="jls-8.html#jls-VariableDeclarator" title="VariableDeclarator">VariableDeclarator</a> {<code class="literal">,</code> <a href="jls-8.html#jls-VariableDeclarator" title="VariableDeclarator">VariableDeclarator</a>}
</div>
</div>
<div class="production"><a name="jls-VariableDeclarator"></a>
<div class="lhs">VariableDeclarator:</div>
<div class="rhs">
<a href="jls-8.html#jls-VariableDeclaratorId" title="VariableDeclaratorId">VariableDeclaratorId</a> [<code class="literal">=</code> <a href="jls-8.html#jls-VariableInitializer" title="VariableInitializer">VariableInitializer</a>]
</div>
</div>
<div class="production"><a name="jls-VariableDeclaratorId"></a>
<div class="lhs">VariableDeclaratorId:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a> [<a href="jls-4.html#jls-Dims" title="Dims">Dims</a>]
</div>
</div>
<div class="production"><a name="jls-VariableInitializer"></a>
<div class="lhs">VariableInitializer:</div>
<div class="rhs">
<a href="jls-15.html#jls-Expression" title="Expression">Expression</a> <br>
<a href="jls-10.html#jls-ArrayInitializer" title="ArrayInitializer">ArrayInitializer</a>
</div>
</div>
</div>
<div id="jls-8.3-120" class="productionset"><a name="jls-8.3-120"></a>
<div class="production"><a name="jls-UnannType"></a>
<div class="lhs">UnannType:</div>
<div class="rhs">
<a href="jls-8.html#jls-UnannPrimitiveType" title="UnannPrimitiveType">UnannPrimitiveType</a> <br>
<a href="jls-8.html#jls-UnannReferenceType" title="UnannReferenceType">UnannReferenceType</a>
</div>
</div>
<div class="production"><a name="jls-UnannPrimitiveType"></a>
<div class="lhs">UnannPrimitiveType:</div>
<div class="rhs">
<a href="jls-4.html#jls-NumericType" title="NumericType">NumericType</a> <br>
<code class="literal">boolean</code>
</div>
</div>
<div class="production"><a name="jls-UnannReferenceType"></a>
<div class="lhs">UnannReferenceType:</div>
<div class="rhs">
<a href="jls-8.html#jls-UnannClassOrInterfaceType" title="UnannClassOrInterfaceType">UnannClassOrInterfaceType</a> <br>
<a href="jls-8.html#jls-UnannTypeVariable" title="UnannTypeVariable">UnannTypeVariable</a> <br>
<a href="jls-8.html#jls-UnannArrayType" title="UnannArrayType">UnannArrayType</a>
</div>
</div>
<div class="production"><a name="jls-UnannClassOrInterfaceType"></a>
<div class="lhs">UnannClassOrInterfaceType:</div>
<div class="rhs">
<a href="jls-8.html#jls-UnannClassType" title="UnannClassType">UnannClassType</a> <br>
<a href="jls-8.html#jls-UnannInterfaceType" title="UnannInterfaceType">UnannInterfaceType</a>
</div>
</div>
<div class="production"><a name="jls-UnannClassType"></a>
<div class="lhs">UnannClassType:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>] <br>
<a href="jls-8.html#jls-UnannClassOrInterfaceType" title="UnannClassOrInterfaceType">UnannClassOrInterfaceType</a> <code class="literal">.</code>
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>}
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
[<a href="jls-4.html#jls-TypeArguments" title="TypeArguments">TypeArguments</a>]
</div>
</div>
<div class="production"><a name="jls-UnannInterfaceType"></a>
<div class="lhs">UnannInterfaceType:</div>
<div class="rhs">
<a href="jls-8.html#jls-UnannClassType" title="UnannClassType">UnannClassType</a>
</div>
</div>
<div class="production"><a name="jls-UnannTypeVariable"></a>
<div class="lhs">UnannTypeVariable:</div>
<div class="rhs">
<a href="jls-3.html#jls-Identifier" title="Identifier">Identifier</a>
</div>
</div>
<div class="production"><a name="jls-UnannArrayType"></a>
<div class="lhs">UnannArrayType:</div>
<div class="rhs">
<a href="jls-8.html#jls-UnannPrimitiveType" title="UnannPrimitiveType">UnannPrimitiveType</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a> <br>
<a href="jls-8.html#jls-UnannClassOrInterfaceType" title="UnannClassOrInterfaceType">UnannClassOrInterfaceType</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a> <br>
<a href="jls-8.html#jls-UnannTypeVariable" title="UnannTypeVariable">UnannTypeVariable</a> <a href="jls-4.html#jls-Dims" title="Dims">Dims</a>
</div>
</div>
</div>
<p class="note">The following production from
<a class="xref" href="jls-4.html#jls-4.3" title="4.3.&nbsp;Reference Types and Values">&sect;4.3</a> is shown here for convenience:
</p>
<div id="d5e11944" class="productionset"><a name="d5e11944"></a>
<div class="productionrecap-note">
<div class="production">
<div class="lhs">Dims:</div>
<div class="rhs">
{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">[</code> <code class="literal">]</code> {{<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a>} <code class="literal">[</code> <code class="literal">]</code>}
</div>
</div>
</div>
</div>
<p class="norm"><a name="jls-8.3-130"></a>The
<span class="emphasis"><em>FieldModifier</em></span> clause is described in
<a class="xref" href="jls-8.html#jls-8.3.1" title="8.3.1.&nbsp;Field Modifiers">&sect;8.3.1</a>.
</p>
<p class="norm-static"><a name="jls-8.3-200"></a>The
<span class="emphasis"><em>Identifier</em></span> in a <span class="emphasis"><em>FieldDeclaration</em></span> may be used in
a name to refer to the field.
</p>
<p class="norm-static"><a name="jls-8.3-210"></a>More than one
field may be declared in a single field declaration by using more than
one declarator; the <span class="emphasis"><em>FieldModifier</em></span>s
and <span class="emphasis"><em>UnannType</em></span> apply to all the declarators in the
declaration.
</p>
<p class="norm-static"><a name="jls-8.3-220"></a>The declared
type of a field is denoted by the <span class="emphasis"><em>UnannType</em></span> that
appears in the field declaration, followed by any bracket pairs that
follow the <span class="emphasis"><em>Identifier</em></span> in the declarator.
</p>
<p class="norm-error"><a name="jls-8.3-300"></a>It is a
compile-time error for the body of a class declaration to declare two
fields with the same name.
</p>
<p class="norm-static"><a name="jls-8.3-310"></a>The scope
and shadowing of a field declaration is specified in
<a class="xref" href="jls-6.html#jls-6.3" title="6.3.&nbsp;Scope of a Declaration">&sect;6.3</a> and <a class="xref" href="jls-6.html#jls-6.4" title="6.4.&nbsp;Shadowing and Obscuring">&sect;6.4</a>.
</p>
<p class="norm-static"><a name="jls-8.3-320"></a>If the class
declares a field with a certain name, then the declaration of that
field is said to <span class="emphasis"><em>hide</em></span> any and all accessible
declarations of fields with the same name in superclasses, and
superinterfaces of the class.
</p>
<p class="note">In this respect, hiding of fields differs from
hiding of methods (<a class="xref" href="jls-8.html#jls-8.4.8.3" title="8.4.8.3.&nbsp;Requirements in Overriding and Hiding">&sect;8.4.8.3</a>), for there is no
distinction drawn between <code class="literal">static</code> and non-<code class="literal">static</code> fields in field
hiding whereas a distinction is drawn between <code class="literal">static</code> and
non-<code class="literal">static</code> methods in method hiding.
</p>
<p class="norm-static"><a name="jls-8.3-330"></a>A hidden
field can be accessed by using 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>) if it is <code class="literal">static</code>, or by using a field
access expression that contains the keyword <code class="literal">super</code>
(<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>) or a cast to a superclass type.
</p>
<p class="note">In this respect, hiding of fields is similar to
hiding of methods.
</p>
<p class="norm-static"><a name="jls-8.3-340"></a>If a field
declaration hides the declaration of another field, the two fields
need not have the same type.
</p>
<p class="norm-static"><a name="jls-8.3-400"></a>A class
inherits from its direct superclass and direct superinterfaces all the
non-private fields of the superclass and superinterfaces that are both
accessible to code in the class and not hidden by a declaration in the
class.
</p>
<p class="norm-static"><a name="jls-8.3-410"></a>A <code class="literal">private</code>
field of a superclass might be accessible to a subclass - for example,
if both classes are members of the same class. Nevertheless, a
<code class="literal">private</code> field is never inherited by a subclass.
</p>
<p class="norm-error"><a name="jls-8.3-420"></a>It is
possible for a class to inherit more than one field with the same
name. Such a situation does not in itself cause a compile-time
error. However, any attempt within the body of the class to refer to
any such field by its simple name will result in a compile-time error,
because such a reference is ambiguous.
</p>
<p class="norm-static"><a name="jls-8.3-430"></a>There might
be several paths by which the same field declaration might be
inherited from an interface. In such a situation, the field is
considered to be inherited only once, and it may be referred to by its
simple name without ambiguity.
</p>
<p class="norm-static"><a name="jls-8.3-500"></a>A value
stored in a field of type <code class="literal">float</code> is always an element of the float
value set (<a class="xref" href="jls-4.html#jls-4.2.3" title="4.2.3.&nbsp;Floating-Point Types, Formats, and Values">&sect;4.2.3</a>); similarly, a value stored in
a field of type <code class="literal">double</code> is always an element of the double value
set. It is not permitted for a field of type <code class="literal">float</code> to contain an
element of the float-extended-exponent value set that is not also an
element of the float value set, nor for a field of type <code class="literal">double</code> to
contain an element of the double-extended-exponent value set that is
not also an element of the double value set.
</p>
<div class="example"><a name="d5e11989"></a><p class="title"><b>Example&nbsp;8.3-1.&nbsp;Multiply Inherited Fields</b></p>
<div class="example-contents">
<p class="note">A class may inherit two or more fields with the same
name, either from two interfaces or from its superclass and an
interface. A compile-time error occurs on any attempt to refer to any
ambiguously inherited field by its simple name. A qualified name or a
field access expression that contains the keyword <code class="literal">super</code>
(<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>) may be used to access such fields
unambiguously. In the program:
</p><pre class="programlisting">
interface Frob { float v = 2.0f; }
class SuperTest { int v = 3; }
class Test extends SuperTest implements Frob {
public static void main(String[] args) {
new Test().printV();
}
void printV() { System.out.println(v); }
}
</pre><p class="note">the class <code class="literal">Test</code> inherits two
fields named <code class="literal">v</code>, one from its
superclass <code class="literal">SuperTest</code> and one from its
superinterface <code class="literal">Frob</code>. This in itself is permitted,
but a compile-time error occurs because of the use of the simple
name <code class="literal">v</code> in method <code class="literal">printV</code>: it
cannot be determined which <code class="literal">v</code> is intended.
</p>
<p class="note">The following variation uses the field access
expression <code class="literal">super.v</code> to refer to the field
named <code class="literal">v</code> declared in
class <code class="literal">SuperTest</code> and uses the qualified
name <code class="literal">Frob.v</code> to refer to the field
named <code class="literal">v</code> declared in
interface <code class="literal">Frob</code>:
</p><pre class="programlisting">
interface Frob { float v = 2.0f; }
class SuperTest { int v = 3; }
class Test extends SuperTest implements Frob {
public static void main(String[] args) {
new Test().printV();
}
void printV() {
System.out.println((super.v + Frob.v)/2);
}
}
</pre><p class="note">It compiles and prints:</p><pre class="screen">
2.5
</pre><p class="note">Even if two distinct inherited fields have the same
type, the same value, and are both <code class="literal">final</code>, any reference to either
field by simple name is considered ambiguous and results in a
compile-time error. In the program:
</p><pre class="programlisting">
interface Color { int RED=0, GREEN=1, BLUE=2; }
interface TrafficLight { int RED=0, YELLOW=1, GREEN=2; }
class Test implements Color, TrafficLight {
public static void main(String[] args) {
System.out.println(GREEN); // compile-time error
System.out.println(RED); // compile-time error
}
}
</pre><p class="note">it is not astonishing that the reference
to <code class="literal">GREEN</code> should be considered ambiguous, because
class <code class="literal">Test</code> inherits two different declarations
for <code class="literal">GREEN</code> with different values. The point of this
example is that the reference to <code class="literal">RED</code> is also
considered ambiguous, because two distinct declarations are
inherited. The fact that the two fields named <code class="literal">RED</code>
happen to have the same type and the same unchanging value does not
affect this judgment.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e12022"></a><p class="title"><b>Example&nbsp;8.3-2.&nbsp;Re-inheritance of Fields</b></p>
<div class="example-contents">
<p class="note">If the same field declaration is inherited from an
interface by multiple paths, the field is considered to be inherited
only once. It may be referred to by its simple name without
ambiguity. For example, in the code:
</p><pre class="programlisting">
interface Colorable {
int RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff;
}
interface Paintable extends Colorable {
int MATTE = 0, GLOSSY = 1;
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {}
class PaintedPoint extends ColoredPoint implements Paintable {
int p = RED;
}
</pre><p class="note">the
fields <code class="literal">RED</code>, <code class="literal">GREEN</code>,
and <code class="literal">BLUE</code> are inherited by the
class <code class="literal">PaintedPoint</code> both through its direct
superclass <code class="literal">ColoredPoint</code> and through its direct
superinterface <code class="literal">Paintable</code>. The simple
names <code class="literal">RED</code>, <code class="literal">GREEN</code>,
and <code class="literal">BLUE</code> may nevertheless be used without ambiguity
within the class <code class="literal">PaintedPoint</code> to refer to the
fields declared in interface <code class="literal">Colorable</code>.
</p>
</div>
</div><br class="example-break"><div class="section" title="8.3.1.&nbsp;Field Modifiers">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="jls-8.3.1"></a>8.3.1.&nbsp;Field Modifiers
</h3>
</div>
</div>
</div>
<div id="jls-8.3.1-100" class="productionset"><a name="jls-8.3.1-100"></a>
<div class="production"><a name="jls-FieldModifier"></a>
<div class="lhs">FieldModifier:</div>
<div class="rhs">
<a href="jls-9.html#jls-Annotation" title="Annotation">Annotation</a> <code class="literal">public</code> <code class="literal">protected</code> <code class="literal">private</code> <br>
<code class="literal">static</code> <code class="literal">final</code> <code class="literal">transient</code> <code class="literal">volatile</code>
</div>
</div>
</div>
<p class="norm-error"><a name="jls-8.3.1-110"></a>The rules for
annotation modifiers on a field declaration are specified in
<a class="xref" href="jls-9.html#jls-9.7.4" title="9.7.4.&nbsp;Where Annotations May Appear">&sect;9.7.4</a> and <a class="xref" href="jls-9.html#jls-9.7.5" title="9.7.5.&nbsp;Multiple Annotations Of The Same Type">&sect;9.7.5</a>.
</p>
<p class="norm-error"><a name="jls-8.3.1-200"></a>It is a
compile-time error if the same keyword appears more than once as a
modifier for a field declaration.
</p>
<p class="note">If two or more (distinct) field modifiers appear in
a field declaration, it is customary, though not required, that they
appear in the order consistent with that shown above in the production
for <span class="emphasis"><em>FieldModifier</em></span>.
</p>
<div class="section" title="8.3.1.1.&nbsp;static Fields">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="jls-8.3.1.1"></a>8.3.1.1.&nbsp;<code class="literal">static</code> Fields
</h4>
</div>
</div>
</div>
<p class="norm-static"><a name="jls-8.3.1.1-100"></a>If a
field is declared <code class="literal">static</code>, there exists exactly one incarnation of
the field, no matter how many instances (possibly zero) of the class
may eventually be created. A <code class="literal">static</code> field, sometimes called a class
variable, is incarnated when the class is initialized
(<a class="xref" href="jls-12.html#jls-12.4" title="12.4.&nbsp;Initialization of Classes and Interfaces">&sect;12.4</a>).
</p>
<p class="norm-static"><a name="jls-8.3.1.1-110"></a>A field
that is not declared <code class="literal">static</code> (sometimes called a non-<code class="literal">static</code> field)
is called an <span class="emphasis"><em>instance variable</em></span>. Whenever a new
instance of a class is created (<a class="xref" href="jls-12.html#jls-12.5" title="12.5.&nbsp;Creation of New Class Instances">&sect;12.5</a>), a new
variable associated with that instance is created for every instance
variable declared in that class or any of its superclasses.
</p>
<div class="example"><a name="d5e12070"></a><p class="title"><b>Example&nbsp;8.3.1.1-1.&nbsp;<code class="literal">static</code> Fields</b></p>
<div class="example-contents"><pre class="programlisting">
class Point {
int x, y, useCount;
Point(int x, int y) { this.x = x; this.y = y; }
static final Point origin = new Point(0, 0);
}
class Test {
public static void main(String[] args) {
Point p = new Point(1,1);
Point q = new Point(2,2);
p.x = 3;
p.y = 3;
p.useCount++;
p.origin.useCount++;
System.out.println("(" + q.x + "," + q.y + ")");
System.out.println(q.useCount);
System.out.println(q.origin == Point.origin);
System.out.println(q.origin.useCount);
}
}
</pre><p class="note">This program prints:</p><pre class="screen">
(2,2)
0
true
1
</pre><p class="note">showing that changing the
fields <code class="literal">x</code>, <code class="literal">y</code>,
and <code class="literal">useCount</code> of <code class="literal">p</code> does not
affect the fields of <code class="literal">q</code>, because these fields are
instance variables in distinct objects. In this example, the class
variable <code class="literal">origin</code> of the
class <code class="literal">Point</code> is referenced both using the class name
as a qualifier, in <code class="literal">Point.origin</code>, and using
variables of the class type 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>), as in <code class="literal">p.origin</code>
and <code class="literal">q.origin</code>. These two ways of accessing
the <code class="literal">origin</code> class variable access the same object,
evidenced by the fact that the value of the reference equality
expression (<a class="xref" href="jls-15.html#jls-15.21.3" title="15.21.3.&nbsp;Reference Equality Operators == and !=">&sect;15.21.3</a>):
</p><pre class="screen">
q.origin==Point.origin
</pre><p class="note">is true. Further evidence is that the
incrementation:
</p><pre class="screen">
p.origin.useCount++;
</pre><p class="note">causes the value
of <code class="literal">q.origin.useCount</code> to be <code class="literal">1</code>;
this is so because <code class="literal">p.origin</code>
and <code class="literal">q.origin</code> refer to the same variable.
</p>
</div>
</div><br class="example-break"><div class="example"><a name="d5e12098"></a><p class="title"><b>Example&nbsp;8.3.1.1-2.&nbsp;Hiding of Class Variables</b></p>
<div class="example-contents"><pre class="programlisting">
class Point {
static int x = 2;
}
class Test extends Point {
static double x = 4.7;
public static void main(String[] args) {
new Test().printX();
}
void printX() {
System.out.println(x + " " + super.x);
}
}
</pre><p class="note">This program produces the output:</p><pre class="screen">
4.7 2
</pre><p class="note">because the declaration of <code class="literal">x</code> in
class <code class="literal">Test</code> hides the definition
of <code class="literal">x</code> in class <code class="literal">Point</code>, so
class <code