---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 15 Edition"
java_version: 15
chapter: "Chapter 8. Classes"
section: "8"
heading: "Chapter 8. Classes"
source: https://docs.oracle.com/javase/specs/jls/se15/html/jls-8.html
id: "8"
url: https://jvm.obfuscated.software/jls/se15/8.html
raw: https://jvm.obfuscated.software/jls/se15/8.md
---

# Chapter 8. Classes

Class declarations define new reference types and describe how they are implemented ([§8.1](8.1.md)).

A *top level class* is a class that is not a nested class.

A *nested class* is any class whose declaration occurs within the body of another class or interface.

This chapter discusses the common semantics of all classes - top level ([§7.6](7.6.md)) and nested (including member classes ([§8.5](8.5.md), [§9.5](9.5.md)), local classes ([§14.3](14.3.md)) and anonymous classes ([§15.9.5](15.9.5.md))). Details that are specific to particular kinds of classes are discussed in the sections dedicated to these constructs.

A named class may be declared `abstract` ([§8.1.1.1](8.1.1.1.md)) 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.1.2](8.1.1.2.md)), in which case it cannot have subclasses. If a class is declared `public`, then it can be referred to from code in any package of its module and potentially from code in other modules. Each class except `Object` is an extension of (that is, a subclass of) a single existing class ([§8.1.4](8.1.4.md)) and may implement interfaces ([§8.1.5](8.1.5.md)). Classes may be *generic* ([§8.1.2](8.1.2.md)), that is, they may declare type variables whose bindings may differ among different instances of the class.

Classes may be decorated with annotations ([§9.7](9.7.md)) just like any other kind of declaration.

The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors ([§8.1.6](8.1.6.md)). The scope ([§6.3](6.3.md)) of a member ([§8.2](8.2.md)) 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 ([§6.6](6.6.md)) `public`, `protected`, or `private`. The members of a class include both declared and inherited members ([§8.2](8.2.md)). 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.

Field declarations ([§8.3](8.3.md)) 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](8.3.1.2.md)), in which case it can be assigned to only once. Any field declaration may include an initializer.

Member class declarations ([§8.5](8.5.md)) describe nested classes that are members of the surrounding class. Member classes may be `static`, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes ([§8.1.3](8.1.3.md)).

Member interface declarations ([§8.5](8.5.md)) describe nested interfaces that are members of the surrounding class.

Method declarations ([§8.4](8.4.md)) describe code that may be invoked by method invocation expressions ([§15.12](15.12.md)). 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 `abstract`. A method may be declared `final` ([§8.4.3.3](8.4.3.3.md)), in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent `native` code ([§8.4.3.4](8.4.3.4.md)). A `synchronized` method ([§8.4.3.6](8.4.3.6.md)) automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a `synchronized` statement ([§14.19](14.19.md)), thus allowing its activities to be synchronized with those of other threads ([§17 (*Threads and Locks*)](17.md)).

Method names may be overloaded ([§8.4.9](8.4.9.md)).

Instance initializers ([§8.6](8.6.md)) are blocks of executable code that may be used to help initialize an instance when it is created ([§15.9](15.9.md)).

Static initializers ([§8.7](8.7.md)) are blocks of executable code that may be used to help initialize a class.

Constructors ([§8.8](8.8.md)) 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.8.8](8.8.8.md)).

**Sections:**

- [8.1. Class Declarations](8.1.md)
- [8.2. Class Members](8.2.md)
- [8.3. Field Declarations](8.3.md)
- [8.4. Method Declarations](8.4.md)
- [8.5. Member Type Declarations](8.5.md)
- [8.6. Instance Initializers](8.6.md)
- [8.7. Static Initializers](8.7.md)
- [8.8. Constructor Declarations](8.8.md)
- [8.9. Enum Types](8.9.md)
