Java Programming/Keywords/final

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

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

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

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.

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us