Java Programming/Keywords/final

From Wikibooks, open books for an open world
Jump to: navigation, 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.
1 private static final long serialVersionUID = -5437975414336623381L;

For a variable[edit]

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.
1 final int a = 1;
2 a = 2;

Example Code section 3: Only modify the value of the object.
1 final ArrayList list = new ArrayList();
2 System.out.println(list.size());
3 list.add("One item");
4 System.out.println(list.size());
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.
1 static final double PI = 3.1415926;

The final keyword can also be used for method parameters:

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

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]

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

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

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

For a method[edit]

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:
1 public class NoOverwriting {
2   public final void sealedMethod() {
3   }
4 }


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.