CHAPTER 8
class 1. The noun class derives from Medieval French
and French classe from Latin classis,
probably originally a summons, hence a summoned collection of
persons, a group liable to be summoned: perhaps for callassis
from calare, to call, hence to summon.
Eric Partridge, Origins: A Short Etymological Dictionary of Modern English
Class declarations define new reference types and describe how they are implemented (§8.1).
The name of a class has as its scope all type declarations in the package in which the class is declared (§8.1.1). A class may be declared abstract
(§8.1.2.1) 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 final
(§8.1.2.2), in which case it cannot have subclasses. If a class is declared public
, then it can be referred to from other packages.
Each class except Object
is an extension of (that is, a subclass of) a single existing class (§8.1.3) and may implement interfaces (§8.1.4).
The body of a class declares members (fields and methods), static initializers, and constructors (§8.1.5). The scope of the name of a member is the entire declaration of the class to which the member belongs. Field, method, and constructor declarations may include the access modifiers (§6.6) public
, protected
, or private
. The members of a class include both declared and inherited members (§8.2). Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface.
Field declarations (§8.3) 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 final
(§8.3.1.2), in which case it cannot be assigned to except as part of its declaration. Any field declaration may include an initializer; the declaration of a final
field must include an initializer.
Method declarations (§8.4) describe code that may be invoked by method invocation expressions (§15.11). 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 the class type. A method whose declaration does not indicate how it is implemented must be declared abstract
. A method may be declared final
(§8.4.3.3), in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent native
code (§8.4.3.4). A synchronized
method (§8.4.3.5) automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized
statement (§14.17), thus allowing its activities to be synchronized with those of other threads (§17).
Method names may be overloaded (§8.4.7).
Static initializers (§8.5) are blocks of executable code that may be used to help initialize a class when it is first loaded (§12.4).
Constructors (§8.6) 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 (§8.6.6).