---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 22 Edition"
java_version: 22
chapter: "Chapter 8. Classes"
section: "8.1.1"
heading: "8.1.1. Class Modifiers"
parent: "8.1. Class Declarations"
source: https://docs.oracle.com/javase/specs/jls/se22/html/jls-8.html#jls-8.1.1
id: "8.1.1"
url: https://jvm.obfuscated.software/jls/se22/8.1.1.html
raw: https://jvm.obfuscated.software/jls/se22/8.1.1.md
---

# 8.1.1. Class Modifiers

A class declaration may include *class modifiers*.

```
ClassModifier:
  (one of)
  Annotation public protected private
  abstract static final sealed non-sealed strictfp
```

The rules concerning annotation modifiers for a class declaration are specified in [§9.7.4](9.7.4.md) and [§9.7.5](9.7.5.md).

The access modifier `public` ([§6.6](6.6.md)) pertains only to top level classes ([§7.6](7.6.md)) and member classes ([§8.5](8.5.md), [§9.5](9.5.md)), not to local classes ([§14.3](14.3.md)) or anonymous classes ([§15.9.5](15.9.5.md)).

The access modifiers `protected` and `private` pertain only to member classes.

The modifier `static` pertains only to member classes and local classes.

It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration, or if a class declaration has more than one of the access modifiers `public`, `protected`, and `private`.

It is a compile-time error if a class declaration has more than one of the modifiers `sealed`, `non-sealed`, and `final`.

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 *ClassModifier*.

## 8.1.1.1. `abstract` Classes

An `abstract` class is a class that is incomplete, or to be considered incomplete.

It is a compile-time error if an attempt is made to create an instance of an `abstract` class using a class instance creation expression ([§15.9.1](15.9.1.md)).

A subclass of an `abstract` class that is not itself `abstract` may be instantiated, resulting in the execution of a constructor for the `abstract` class and, therefore, the execution of the field initializers for instance variables of that class.

A normal class may have `abstract` methods, that is, methods that are declared but not yet implemented ([§8.4.3.1](8.4.3.1.md)), only if it is an `abstract` class. It is a compile-time error if a normal class that is not `abstract` has an `abstract` method.

A class C has `abstract` methods if either of the following is true:

- Any of the member methods ([§8.2](8.2.md)) of C - either declared or inherited - is `abstract`.
- Any of C's superclasses has an `abstract` method declared with package access, and there exists no method that overrides the `abstract` method from C or from a superclass of C.

It is a compile-time error to declare an `abstract` class type such that it is not possible to create a subclass that implements all of its `abstract` methods. This situation can occur if the class would have as members two `abstract` methods that have the same method signature ([§8.4.2](8.4.2.md)) but return types for which no type is return-type-substitutable with both ([§8.4.5](8.4.5.md)).

**Example 8.1.1.1-1. Abstract Class Declaration**

```
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() { }
}
```

Here, a class `Point` is declared that must be declared `abstract`, because it contains a declaration of an `abstract` method named `alert`. The subclass of `Point` named `ColoredPoint` inherits the `abstract` method `alert`, so it must also be declared `abstract`. On the other hand, the subclass of `Point` named `SimplePoint` provides an implementation of `alert`, so it need not be `abstract`.

The statement:

```
Point p = new Point();
```

would result in a compile-time error; the class `Point` cannot be instantiated because it is `abstract`. However, a `Point` variable could correctly be initialized with a reference to any subclass of `Point`, and the class `SimplePoint` is not `abstract`, so the statement:

```
Point p = new SimplePoint();
```

would be correct. Instantiation of a `SimplePoint` causes the default constructor and field initializers for `x` and `y` of `Point` to be executed.

**Example 8.1.1.1-2. Abstract Class Declaration that Prohibits Subclasses**

```
interface Colorable {
    void setColor(int color);
}
abstract class Colored implements Colorable {
    public abstract int setColor(int color);
}
```

These declarations result in a compile-time error: it would be impossible for any subclass of class `Colored` to provide an implementation of a method named `setColor`, taking one argument of type `int`, that can satisfy both `abstract` method specifications, because the one in interface `Colorable` requires the same method to return no value, while the one in class `Colored` requires the same method to return a value of type `int` ([§8.4](8.4.md)).

A class type should be declared `abstract` 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 ([§8.8.10](8.8.10.md)) of no arguments, make it `private`, never invoke it, and declare no other constructors. A class of this form usually contains class methods and variables.

The class `Math` is an example of a class that cannot be instantiated; its declaration looks like this:

```
public final class Math {
    private Math() { }  // never instantiate this class
    . . . declarations of class variables and methods . . .
}
```

## 8.1.1.2. `sealed`, `non-sealed`, and `final` Classes

A class can be declared `sealed` if all its direct subclasses are known when the class is declared ([§8.1.6](8.1.6.md)), and no other direct subclasses are desired or required.

Explicit and exhaustive control over a class's direct subclasses is useful when the class hierarchy is used to model the kinds of values in a domain, rather than as a mechanism for code inheritance and reuse. The direct subclasses may themselves be declared `sealed` in order to further control the class hierarchy.

A class can be declared `final` if its definition is complete and no subclasses are desired or required.

It is a compile-time error if a class is declared both `final` and `abstract`, because the implementation of such a class could never be completed ([§8.1.1.1](8.1.1.1.md)).

Because a `final` class never has any subclasses, the methods of a `final` class are never overridden ([§8.4.8.1](8.4.8.1.md)).

A class is *freely extensible* if its direct superclass is not `sealed` ([§8.1.4](8.1.4.md)), and none of its direct superinterfaces are `sealed` ([§8.1.5](8.1.5.md)), and it is neither `sealed` nor `final` itself.

A class that has a `sealed` direct superclass or a `sealed` direct superinterface is freely extensible if and only if it is declared `non-sealed`.

It is a compile-time error if a class has a `sealed` direct superclass or a `sealed` direct superinterface, and is not declared `final`, `sealed`, or `non-sealed` either explicitly or implicitly.

Thus, an effect of the `sealed` keyword is to force all direct subclasses to explicitly declare whether they are `final`, `sealed`, or `non-sealed`. This avoids accidentally exposing a sealed class hierarchy to unwanted subclassing.

An enum class is either implicitly `final` or implicitly `sealed`, so it can implement a `sealed` interface. Similarly, a record class is implicitly `final`, so it can also implement a sealed interface.

It is a compile-time error if a class is declared `non-sealed` but has neither a `sealed` direct superclass nor a `sealed` direct superinterface.

Thus, a subclass of a `non-sealed` class cannot itself be declared `non-sealed`.

## 8.1.1.3. `strictfp` Classes

The `strictfp` modifier on a class declaration is obsolete and should not be used in new code. Its presence or absence has no effect at compile time or run time.

## 8.1.1.4. `static` Classes

The `static` modifier specifies that a nested class is not an inner class ([§8.1.3](8.1.3.md)). Just as a `static` method of a class has no current instance of the class in its body, a `static` nested class has no immediately enclosing instance in its body.

References from a `static` nested class to type parameters, instance variables, local variables, formal parameters, exception parameters, or instance methods of lexically enclosing class, interface, or method declarations are disallowed ([§6.5.5.1](6.5.5.1.md), [§6.5.6.1](6.5.6.1.md), and [§15.12.3](15.12.3.md)).

The `static` modifier does not pertain to all nested classes. It pertains only to member classes, whose declarations may use the `static` modifier, and not to local classes or anonymous classes, whose declarations may not use the `static` modifier ([§14.3](14.3.md), [§15.9.5](15.9.5.md)). However, some local classes are implicitly `static`, namely local enum classes and local record classes, because all nested enum classes and nested record classes are implicitly `static` ([§8.9](8.9.md), [§8.10](8.10.md)).
