Java Programming/Keywords/final

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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 static keyword.

Example Code section 1: Keyword order.
private static final long serialVersionUID = -5437975414336623381L;

For a variable[edit | edit source]

The final 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.

Warning Code section 2: Forbidden double assignment.
final int a = 1;
a = 2;
Example Code section 3: Only modify the value of the object.
final ArrayList list = new ArrayList();
list.add("One item");
Standard input or output Console for Code section 3

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

Example Code section 4: Pi constant.
static final double PI = 3.1415926;

The final keyword can also be used for method parameters:

Example Code section 5: Final method parameter.
public int method(final int inputInteger) {
   int outputInteger = inputInteger + 1;
   return outputInteger;

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 final keyword is similar to const in other languages and the readonly keyword in C#. A final variable cannot be volatile.

For a class[edit | edit source]

The final keyword forbids the creation of a subclass. It is the case of the Integer or String class.

Computer code Code listing 1:
public final class SealedClass {
  public static void main(String[] args) {

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

For a method[edit | edit source]

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

Computer code Code listing 2:
public class NoOverwriting {
  public final void sealedMethod() {

Interest[edit | edit source]

The final keyword is mostly used to guarantee a good usage of the code. For instance (non-static) 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.