Class syntax
>>-+---------+-final-class-Identifier-+-------------------+-> +-public--+ '-extends-ClassType-' '-private-' >-+------------------------------+-ClassBody->< '-implements-InterfaceTypeList-'
Field syntax
>>-+-----------+-final-+--------+-+-----------+-Type-> +-public----+ '-static-' '-transient-' +-protected-+ '-private---' >-+-Identifier--------------+-=-+-Expression-------+-;->< | v-----' | '-ArrayIdentifier-+-[-]-+-' '-ArrayInitializer-'
Method syntax
>>-+-----------+-final-+--------+-+--------------+-+--------+-> +-public----+ '-static-' '-synchronized-' '-native-' +-protected-+ '-private---' >-ResultType-Identifier-(-+---------------------+-)-> '-FormalParameterList-' >-+----------------------+-MethodBody->< '-throws-ClassTypeList-'
Description
A class can be declared final if its definition is complete and if
it requires no subclasses. The final keyword is placed after any protection
modifier such as private or public. If the name of a final class
appears in the extends clause of another class declaration, a compilation
error occurs. If a class is declared both final and abstract, a
compilation error occurs because the implementation of such a class could never be
completed.
A field can be declared final, but its declarator must include a variable
initializer; otherwise a compilation error occurs. Both class and instance variables
(static and non-static fields) may be declared final. Declaring a field final
can serve as useful documentation that its value will not change, can help to avoid
programming errors, and can make it easier for a compiler to generate efficient code.
Any attempt to assign a value to a final field results in a compilation error. Therefore, once a final field has been initialized, it always contains the same value. If a final field holds a reference to an object, then the state of the object may be changed by operations on the object, but the field always refers to the same object. This applies also to arrays, because arrays are objects; if a final field holds a reference to an array, then the components of the array may be changed by operations on the array, but the field always refers to the same array.
Constants in Java can only be defined using the modifier final. You must use final before a variable declaration and you must include an initial value for that variable.
A method can be declared final to prevent subclasses from overriding or hiding it. Attempting to override or hide a final method, or to declare a final method abstract, causes a compilation error to occur. All methods declared in a final class are implicitly final, because it is impossible to override them. It is permitted but not required for the declarations of such methods to redundantly include the final keyword.
Example
In the following example, the class Point declares a final field origin:
class Point { int x, y; int useCount; Point(int x, int y) { this.x = x; this.y = y; } final Point origin = new Point(0, 0); }
The origin field holds a reference to an object that is an instance of class Point whose coordinates are (0, 0). The value of the variable Point.origin can never change, so it always refers to the same Point object, which is the one created by its initializer. However, an operation on this Point object might change its state, for example modifying its useCount or even, misleadingly, its x or y coordinate.
The String class is a good example of a final class; it is a core class and should never be subclassed or modified.
Syntax diagrams
class keyword
extends keyword
implements keyword
native keyword
private keyword
protected keyword
public keyword
static keyword
synchronized keyword
transient keyword
volatile keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.