Java final Keyword Syntax

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

I. final Classes and Methods

Official Documentation

Source: Java Official Documentation -> Writing Final Classes and Methods
You can declare some or all of a class’s methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. For example, you might want to make the getFirstPlayer method in this ChessAlgorithm class final:

1
2
3
4
5
6
7
8
class ChessAlgorithm {
    enum ChessPlayer { WHITE, BLACK }
    ...
    final ChessPlayer getFirstPlayer() {
        return ChessPlayer.WHITE;
    }
    ...
}

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

Summary

A final Java class or method cannot be inherited.

Examples

Class (Integer source code):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package java.lang;

import java.lang.annotation.Native;
// import ...
import static java.lang.String.UTF16;

public final class Integer extends Number // Final class cannot be inherited, but can extend or implement other non-final classes
        implements Comparable<Integer>, Constable, ConstantDesc {
    @Native public static final int   MIN_VALUE = 0x80000000;
    @Native public static final int   MAX_VALUE = 0x7fffffff;
    @SuppressWarnings("unchecked")
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
    // Omitted n lines here
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    @Native private static final long serialVersionUID = 1360826667806852920L;
}

Method:

1
2
3
4
5
6
7
8
class ChessAlgorithm {
    enum ChessPlayer { WHITE, BLACK }
    // ...
    final ChessPlayer getFirstPlayer() { // Cannot be overridden, but can be called
        return ChessPlayer.WHITE;
    }
    // ...
}

II. final Attributes/Variables

final attributes or variables in Java are similar to const variables in C/C++ and cannot be modified.
Examples:

1
2
3
4
5
6
7
8
9
public class Information {
    private static final int WIDTH = 170, HEIGHT = 135; // final attribute, can be private
    public static final int SIZE = WIDTH * HEIGHT; // can also be public

    public int getDifference() {
        final int difference = WIDTH - HEIGHT; // final variable in method
        return difference;
    }
}
Built with Hugo
Theme Stack designed by Jimmy