---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 10 Edition"
java_version: 10
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/se10/html/jls-8.html#jls-8.1.1
id: "8.1.1"
url: https://jvm.obfuscated.software/jls/se10/8.1.1.html
raw: https://jvm.obfuscated.software/jls/se10/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 strictfp
```

The rules for annotation modifiers on 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)), 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 within a directly enclosing class declaration ([§8.5](8.5.md)).

The modifier `static` pertains only to member classes ([§8.5.1](8.5.1.md)), not to top level or local or anonymous 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` ([§6.6](6.6.md)).

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. `final` Classes

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 the name of a `final` class appears in the `extends` clause ([§8.1.4](8.1.4.md)) of another class declaration; this implies that a `final` class cannot have any subclasses.

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)).

## 8.1.1.3. `strictfp` Classes

The effect of the `strictfp` modifier is to make all `float` or `double` expressions within the class declaration (including within variable initializers, instance initializers, static initializers, and constructors) be explicitly FP-strict ([§15.4](15.4.md)).

This implies that all methods declared in the class, and all nested types declared in the class, are implicitly `strictfp`.
