Java Programming/Keywords/final

is a keyword. Beware! It has distinct meanings depending whether it is used for a class, a method, or for a variable. It must be placed before the variable type or the method return type. It is recommended to place it after the access modifier and after the keyword.

For a variable
The  keyword only allows a single assignment for the variable. That is to say, once the variable has been assigned, its value is in read-only. If the variable is a primitive type, its value will no longer change. If it is an object, only its reference will no longer change. Keep in mind that its value can still be changed.

A final variable is often used for universal constants, such as pi:

The  keyword can also be used for method parameters:

It is useful for methods that use side effects to update some objects. Such methods modify the content of an object passed in parameter. The method caller will receive the object update. This will fail if the object parameter has been reassigned in the method. Another object will be updated instead. Final method parameter can also be used to keep the code clean.

The  keyword is similar to   in other languages and the   keyword in C#. A final variable cannot be.

For a class
The  keyword forbids the creation of a subclass. It is the case of the  or   class.

A final class cannot be. The  keyword is similar to   keyword in C#.

For a method
The  keyword forbids to overwrite the method in a subclass. It is useless if the class is already final and a private method is implicitly. A final method cannot be.

Interest
The  keyword is mostly used to guarantee a good usage of the code. For instance (non- ) methods, this allows the compiler to expand the method (similar to an inline function) if the method is small enough. Sometimes it is required to use it. For instance, a nested class can only access the members of the top-level class if they are final.
 * See also Access Modifiers.